ios - 如何动态切换屏幕

标签 ios swift

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

例如这个函数是当登录完成时

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
    }     
}

最佳答案

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

在 Storyboard 中,将一个从登录屏幕连接到 OTP 屏幕的 segue,以及从登录屏幕到主屏幕的另一个 segue。请记住从 Storyboard中的 Controller 开始拖动,而不是 Controller 中的任何 View 。

在右侧面板中为每个 segue 指定一个标识符。我将第一个转场(登录 -> OTP)称为“showOTP”,将第二个转场(登录 -> 主屏幕)称为“showHome”。

在if语句中:

func LoginDone() {

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

}

这里我使用data 作为sender 参数。请将其替换为您要发送到其他 View Controller 的数据。

然后,覆盖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.
    }
}

关于ios - 如何动态切换屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44016811/

相关文章:

json - Google API的可解码结构

iphone - 应用程序在后台运行时如何获取当前位置

快速收缩动画

ios - 如何将已编程的 ViewController 与 Storyboard中创建的新 View 连接起来?

ios - 删除自定义导航栏下的行

ios - 如何根据collectionView中的条件隐藏textview及其高度?

ios - SwiftUI:有条件地隐藏 View 而不重新创建它

ios - 如何在 UIPageViewController 中从一页动画到另一页时设置 View 的背景颜色

iphone - 如何更改 iPhone 的默认浏览器?

ios - pickerView 不同的行高