Search code examples
iosswiftsegueviewdidload

Segue at launch


I'm creating a basic user page where I automatically want to segue to another UIViewController at launch if the user isn't registered.

I created a bool which is set to false, then in the viewDidLoad i added this:

if (userIsRegistered == false) {
        performSegueWithIdentifier("signUp", sender: nil)
    }

Fairly basic I know but it doesn't seem to do the trick.

to make sure the segue code works I just added a UIButton and did an IBAction with the same code and then it works fine. however as I mentioned I want to do the segue at launch if my variable userIsRegistered is set to false.


Solution

  • It doesn't work because it's performed in viewDidLoad, in which all segues components may not be available. For example if you're using a push segue, the navigationController property may not be set yet, or the navigation controller isn't ready, ...

    To have it work, you can try to do it in the viewDidAppear: method but in that case you'll have to take care about segues animations so that it displays correctly.

    To avoid animation issues and because I think it's more logical, you may want to check if the user is registered before creating this controller:

    • user registered = show your "basic user page" controller
    • user unregistered = show your signup controller