C.Islas
C.Islas

Reputation: 59

ViewController not performing segue programmatically

I've encountered something that I've never seen. The scenario is as follows. My initial VC has an enroll button that when hit bring up a couple of separate view controllers deal with the enrollment procedure (I ask for user credentials, and if required by the server, I ask for a verification code). When this process is complete, I save a custom object to NSUserDefaults that is needed later. Then the initial VC is shown again.

Basically, I'm making sure I'm in the main thread and that the custom object is saved. If those conditions are met, I then push the main part of the app.

Here is the relevant code, all my logs are displayed in the output window, but the segue does not happen.

- (void)viewDidLoad {
   [super viewDidLoad];
   self.complete = false;
   if([NSThread isMainThread]){
       NSLog(@"Main Thread-Yes");
       if ([[NSUserDefaults standardUserDefaults] objectForKey:@"CUSTOMOBJECT"]) {
           NSLog(@"Will begin pushToAuthenticate");
           [self pushAuthenticateController];
       }
   } else {
       NSLog(@"Main Thread-No");
   }
} 

- (void)pushAuthenticateController{
   [self performSegueWithIdentifier:@"pushMainVC" sender:self];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if ([segue.identifier isEqualToString:@"pushMainVC"]) {
       NSLog(@"PrepareForSegue-pushMainVC");
       [segue.destinationViewController.navigationItem setHidesBackButton:YES];
       [segue.destinationViewController.navigationItem setTitle:@"MainVC"];
    }
}

In storyboard I have a segue that connects the initial VC with the MainVC, the identifier for that segue matches "pushMainVC", and if I close and reopen the app (after enrolling) the segues does occur. The console output for both instances (reloading the initial VC after enrolling and closing and reopening the app) are as follow:

APPNAME[4106:1628238] Main Thread-Yes

APPNAME[4106:1628238] Will begin pushToAuthenticate

APPNAME[4106:1628238] PrepareForSegue-pushMainVC

Any idea why this could happen, any small thing I'm missing, or any help will be greatly appreciated. Thank you so much.

Upvotes: 0

Views: 124

Answers (2)

AmirBenSalomon
AmirBenSalomon

Reputation: 9

You cant try to load it from the storayboard itself. like this:

    pushMainVC* pushMainV = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([pushMainVC class])];

    [self.navigationController pushViewController:pushMainV animated:YES];

NOTICE: Dont forget to give your viewcontroller Storyboard ID.

Upvotes: 0

matt
matt

Reputation: 535890

You are attempting to perform a push segue from viewDidLoad. But you can't do that because it's too soon; viewDidLoad means that the view exists but is not yet in the interface. You need to wait until there is an interface so you have something to push from.

Upvotes: 1

Related Questions