Search code examples
iosxcodeseguepushviewcontroller

How to use prepareForSegue and avoid errors when using pushViewController?


My segues were added in the storyboard. I want push to a viewController, say Deep_VC, different from the next one in the storyboard, say Next_VC. I'm using:

Deep_VC *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DeepVC"];
[self.navigationController pushViewController:controller animated:YES];

It goes there, but I get errors. The nav bar tile is for the Next_VC, not the Deep_VC, and the error messages:

Finishing up a navigation transition in an unexpected state . . .
Unbalanced calls to begin/end appearance transitions . . .

My prepareForSegue looks like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"toNextVC"]) {
        Next_VC *vc = [segue destinationViewController];
        vc.receivedData = [NSMutableArray arrayWithObjects:passData, nil];
    }
    if ([[segue identifier] isEqualToString:@"toDeepVC"]) {
        Deep_VC *vc = [segue destinationViewController];
        vc.receivedData = [NSMutableArray arrayWithObjects:passData, nil];
    }
}

Can anyone help, please?


Solution

  • I'd rather use performSegue instead of pushViewController in your case. But wait, if you`ve wired your VCs in the storyboard you don't need to push. This will be done automagically by the segue.

    EDIT

    if (login == successful) {
           [self performSegueWithIdentifier:@"toNextVC" sender:self];
    } else {
           [self performSegueWithIdentifier:@"toDeepVC" sender:self];
    }
    

    After that prepareForSegue will be called automatically and there you set your data. pushViewController is not needed as the seque pushes the appropriate VC.

    Hope that helps!