简体   繁体   English

Swift 5 编程转场未触发

[英]Swift 5 programmatic segue not firing

I am trying to programmatically trigger a segue to a new View Controller based on whether or not a user is logged in. I have based my code on the answer at Performing a completion handler before app launches我正在尝试根据用户是否登录以编程方式触发新的视图控制器的 segue。我的代码基于在应用程序启动之前执行完成处理程序的答案

I've created a text button with the segue but it never fires.我用 segue 创建了一个文本按钮,但它永远不会触发。 The print("Logged In") line functions, just not the segue. print("Logged In") 行功能,而不是 segue。 I want my buttons to be disabled to prevent accidental taps, but having it enabled or disabled makes no difference to its firing.我希望我的按钮被禁用以防止意外点击,但启用或禁用它对其触发没有影响。

My code in InitialViewController.swift:我在 InitialViewController.swift 中的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    checkLoginStatus()
}

func checkLoginStatus() {
    let defaults = UserDefaults.standard
    let isLoggedIn = defaults.bool(forKey: "loggedIn")

    if (isLoggedIn == true) {
        print("Logged In")
        self.performSegue(withIdentifier: "HomeController", sender: self)
    }

    else {
        print("Not Logged In")
        self.performSegue(withIdentifier: "LoginController", sender: self)
    }
}

You can attach a segue from one view controller to another one in Storyboard this way:您可以通过这种方式将一个视图控制器的 segue 附加到 Storyboard 中的另一个视图控制器:

  1. Tap and hold right-click from View Controller property to assign it to another view controller inside of your storyboard this way:View Controller属性中点击并按住右键,以这种方式将其分配给故事板内的另一个视图控制器:

在此处输入图片说明

  1. Choose the type of presentation in Manual Segue section and tap on it:Manual Segue部分选择演示类型并点击它:

在此处输入图片说明

  1. Give an identifier to the newly created segue ("HomeController" or "LoginController" in your case):为新创建的 segue 提供一个标识符(在您的情况下为“HomeController”或“LoginController”):

在此处输入图片说明

  1. Remove your old segue that is triggered on a button tap.删除在按钮点击时触发的旧转场。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM