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
performSegueWithIdentifier is being skipped *SWIFT
提问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 viewWillAppear
or viewDidAppear
method.
Then it should use properly.
您应该将代码移到viewWillAppear
orviewDidAppear
方法中。那么它应该正确使用。
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. 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.
此外,viewDidLoad
不会在每次出现视图时调用该方法,而是仅在第一次加载时调用该方法。例如,在您执行赛格瑞并返回给视图,它不会再次加载,但只要求viewWillAppear
和viewDidAppear
试。
回答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.
没有更多信息就很难说。
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.
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.
您是否有按钮或已建立转场的东西,并且您是否提供了转场的标识符?我在 viewDidLoad 中做了一个类似的调用并且它起作用了。
您是否将视图控制器嵌入到 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