如何动态切换屏幕

时间:2017-05-17 06:06:20

标签: ios swift

我是IOS开发的新手......我来自android背景 我想在xcode中实现登录... 当用户点击登录按钮并且如果成功,对于first login它应该进入OTP屏幕并且在用户是注册用户之后......每当他/她点击登录按钮时它应该回家屏幕... 所以基本上我希望login按钮切换到不同的屏幕(没有导航)

例如,此func是登录完成时的

func LoginDone()
{
    if (registeredUser()){
     //switch to OTP screen and also send The username to that .swift file
    }

    else{
     //switch to homescreen and send some data to that .swift file
    }     
}

1 个答案:

答案 0 :(得分:1)

您可以从登录屏幕连接两个segue。

在故事板中,将登录界面中的segue连接到OTP屏幕,将另一个segue从登录屏幕连接到主屏幕。请记住从故事板中的控制器开始拖动,控制器中的任何视图。

在右侧面板中为每个segue指定一个标识符。我将调用第一个segue(登录 - >> OTP)" showOTP"和第二个segue(登录 - >主屏幕)" showHome"。

在if语句中:

func LoginDone() {

    if (registeredUser()){
         performSegue(withIdentifier: "showOTP", sender: data)
    } else {
         performSegue(withIdentifier: "showHome", sender: data)
    }

}

这里我使用data作为sender参数。请将其替换为您要发送给其他视图控制器的数据。

然后,覆盖prepareForSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showOTP" {
        let vc = segue.destination as! OTPController // Here replace OTPController with the class name of the OTP screen
        vc.username = sender as! String // username is a property in OTPController used to accept the value passed to it. If you don't have this, declare it.
    } else if segue.identifier == "showHome" {
        let vc = segue.destination as! HomeController // Here replace HomeController with the class name of the home screen
        vc.data = sender as! SomeType // data is a property in HomeController used to accept the value passed to it. If you don't have this, declare it.
        // replace SomeType with the type of data you are passing.
    }
}