简体   繁体   English

正在跳过performSegueWithIdentifier * SWIFT

[英]performSegueWithIdentifier is being skipped *SWIFT

I have a UITableViewController that I would like people to access as the main page. 我有一个UITableViewController,我希望人们可以作为主页面访问。 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语句检查用户是否已经登录过。 If not, I would like to perform a segue to the login view controller. 如果没有,我想对登录视图控制器执行segue。 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. 然而,当我运行应用程序时,它会通过if语句,识别出用户是== nil,然后继续执行performSegueWithIdentifier操作。

override func viewDidLoad() {
        super.viewDidLoad()

        if user == nil {

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

        }
}

Any idea why? 知道为什么吗?

You should move your code into the viewWillAppear or viewDidAppear method. 您应该将代码移动到viewWillAppearviewDidAppear方法中。 Then it should use properly. 那应该正确使用。

But now the view flickers a short time. 但现在这种观点在短时间内闪烁。 To remove that flickering, just hide the view in your viewWillApear method: 要消除这种闪烁,只需在viewWillApear方法中隐藏视图:

self.view.hidden = true

Also the viewDidLoad method doesn't get called everytime the view appears but only the first time it loads. 此外,每次出现视图时都不会调用viewDidLoad方法,但只会在第一次加载时调用。 If you for example perform a segue and return back to the view, it doesn't load again but only calls viewWillAppear and viewDidAppear again. 例如,如果执行segue并返回视图,则不会再次加载,而只会再次调用viewWillAppearviewDidAppear

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

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? 你有一个按钮或已经建立了segue的东西,你有没有提供segue的标识符? I made a similar call in viewDidLoad and it worked. 我在viewDidLoad中做了一个类似的调用,它起作用了。

  2. Have you embedded the view controller in a UINavigationController? 您是否在UINavigationController中嵌入了视图控制器? If not, performing the segue is not possible. 如果不是,则不可能执行segue。 It sounds like you're using a UITableViewController, which inherits from UIViewController. 听起来你正在使用UITableViewController,它继承自UIViewController。 So this may be the issue. 所以这可能是问题所在。

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提供一个像ViewControllerClassId的ID

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

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