Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segue call from viewDidAppear works, but not from viewWillAppear

Tags:

ios

swift

segue

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.

like image 293
djilo Avatar asked May 27 '16 23:05

djilo


1 Answers

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.

like image 73
Pranav Wadhwa Avatar answered Sep 22 '22 13:09

Pranav Wadhwa