Segue call from viewDidAppear works, but not from viewWillAppear

妖精的绣舞 提交于 2020-01-14 09:36:06

问题


I have the following scenario with the given environment:

Xcode v7.3.1
Swift 2

From within a login viewController, I initially had the following segue call in viewWillAppear method to bypass login screen if the user is already logged in:

  override func viewWillAppear(animated: Bool) {
    if PFUser.currentUser() != nil {
      self.performSegueWithIdentifier("login", sender: self)
    }
  }

The segue was not working, so I set a breakpoint where the segue call is being made and verified that it is indeed being hit. I also step through the call and verify no exceptions are thrown, but for some reason the segue is not performed.

However, I put the same call within the viewDidAppear method:

  override func viewDidAppear(animated: Bool) {
    if PFUser.currentUser() != nil {
      self.performSegueWithIdentifier("login", sender: self)
    }
  }

Again, I verified that the segue call is being hit, but in this case the segue is actually performed. So I'm wondering why the exact same call doesn't work when it is invoked in the viewWillAppear method but does work as expected within viewDidAppear. The most puzzling part is that it appears that the call is being made, but the expected behavior is not observed. I tried Googling for anyone else running into this problem but was not able to find anything.

I guess I can live with it being called from viewWillAppear, but there is that brief appearance of the login screen before being redirected, which is slightly annoying. If anyone has a solution or suggestions on how I might be able to get it to work from viewWillAppear I would be greatly appreciative.


回答1:


You cannot perform segues in the viewWillAppear method. This is because the view has not fully appeared yet, and it cannot transition over to another view. In this scenario, I would have a load image or something similar and have it fade away if the segue is not supposed to be performed. This, of course, would be done in the viewDidAppear method.



来源:https://stackoverflow.com/questions/37493564/segue-call-from-viewdidappear-works-but-not-from-viewwillappear

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!