Search code examples
iosxcodeuiviewcontrolleruinavigationcontrolleruinavigationitem

Redirect to new view controller in the middle of navigation stack


I am fairly new to navigation view controllers and stacks in iOS. Please help me with this, tried searching for a long time couldn't find anything relevant.

I have a LoginViewController, a SWRevealViewController for side menu operation, a FirstViewController (the first one in navigation stack) and a SecondViewController (second in navigation stack).

I have to do the following in LoginViewController, assume login and found are two boolean variables:

if (login && !found) {
//redirect to FirstViewController : the following works
[self presentViewController:swReveal animated:NO completion:nil];
}
else if (login && found) {
//redirect to SecondViewController
}

Please help me fill the else if. Really appreciate your time!


Solution

  • So based on what I understood from your comments, FirstViewController is a subclass of TableViewController and SecondViewController is the DetailView.

    So based on that, the best approach for "redirecting" to the DetailView (SecondViewController), would be creating a segue on the Storyboard, and then calling that segue programatically once the FirstViewController is loaded (in its viewDidLoad)

    On both cases you would first show the FirstViewController and then depending on the if statement, you would either stay or go to the SecondViewController.

    So first step: Create the segue from the Storyboard, and give it an identifier.

    Second: Perform that segue programatically when the FirstViewController is loaded (and of course, the if statement is valid) by using:

    [self performSegueWithIdentifier: @"MySegue" sender: self];
    

    More on that subject:

    Alternative ways to push view controllers with storyboard programmatically

    Creating a segue programmatically