xcode 正在跳过 performSegueWithIdentifier *SWIFT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28572379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:40:19  来源:igfitidea点击:

performSegueWithIdentifier is being skipped *SWIFT

iosxcodeswiftsegue

提问by Octavio Antonio Cede?o

I have a UITableViewController that I would like people to access as the main page. I do have a login view controller but I do not want this to be my initial view controller. I have an if statement checking if the user has logged in previously. If not, I would like to perform a segue to the login view controller. When I run the app however, it goes through the if statement, recognizes that the user is == nil, and then goes right over the performSegueWithIdentifier operation.

我有一个 UITableViewController,我希望人们将其作为主页访问。我确实有一个登录视图控制器,但我不希望它成为我的初始视图控制器。我有一个 if 语句检查用户之前是否登录过。如果没有,我想对登录视图控制器执行 segue。但是,当我运行该应用程序时,它会通过 if 语句,识别出用户是 == nil,然后直接执行 performSegueWithIdentifier 操作。

override func viewDidLoad() {
        super.viewDidLoad()

        if user == nil {

            self.performSegueWithIdentifier("LoginSegue", sender: self)

        }
}

Any idea why?

知道为什么吗?

回答by Christian

You should move your code into the viewWillAppearor viewDidAppearmethod. Then it should use properly.

您应该将代码移到viewWillAppearorviewDidAppear方法中。那么它应该正确使用。

But now the view flickers a short time. To remove that flickering, just hide the view in your viewWillApearmethod:

但现在,视线闪烁了很短的时间。要消除闪烁,只需在您的viewWillApear方法中隐藏视图:

self.view.hidden = true

Also the viewDidLoadmethod doesn't get called everytime the view appears but only the first time it loads. If you for example perform a segue and return back to the view, it doesn't load again but only calls viewWillAppearand viewDidAppearagain.

此外,viewDidLoad不会在每次出现视图时调用该方法,而是仅在第一次加载时调用该方法。例如,在您执行赛格瑞并返回给视图,它不会再次加载,但只要求viewWillAppearviewDidAppear试。

回答by Dx_

For Swift 3.0, try to run on the main thread like this.

对于 Swift 3.0,尝试像这样在主线程上运行。

DispatchQueue.main.async {
    self.performSegue(withIdentifier: "LoginSegue", sender: self)
}

回答by st.derrick

Without more information it is hard to say.

没有更多信息就很难说。

  1. Do you have a button or something with an already established segue, and have you provided the identifier for the segue? I made a similar call in viewDidLoad and it worked.

  2. Have you embedded the view controller in a UINavigationController? If not, performing the segue is not possible. It sounds like you're using a UITableViewController, which inherits from UIViewController. So this may be the issue.

  1. 您是否有按钮或已建立转场的东西,并且您是否提供了转场的标识符?我在 viewDidLoad 中做了一个类似的调用并且它起作用了。

  2. 您是否将视图控制器嵌入到 UINavigationController 中?如果没有,则无法执行转场。听起来您正在使用从 UIViewController 继承的 UITableViewController。所以这可能是问题所在。

回答by Hamid Shahsavari

dispatch_async(dispatch_get_main_queue(), { () -> Void in   
        let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewControllerClassId") as! ViewControllerClassName
        self.presentViewController(viewController, animated: true, completion: nil)
    })

this code solved my problem. use my code instead of self.performSegueWithIdentifier("LoginSegue", sender: self) and give your destination ViewController an ID like ViewControllerClassId

这段代码解决了我的问题。使用我的代码而不是 self.performSegueWithIdentifier("LoginSegue", sender: self) 并为您的目标 ViewController 提供一个 ID,例如 ViewControllerClassId