7

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
  • 1
    I would suggest that your default presentation is the "logged in" scene and that you show the log in scene modally on top of it when required
    – Paulw11
    Commented May 27, 2016 at 23:21

1 Answer 1

11

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.

2
  • Ok, I thought it might be something like this. Thanks for the info and recommendation.
    – djilo
    Commented May 28, 2016 at 0:03
  • @djilo No problem! Commented May 28, 2016 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.