Search code examples
iosios5uiviewuiviewcontrolleruinavigationcontroller

iOS: How to programmatically navigate/jump to different scenes managed by Navigation Controller?


I have a series of views that are managed under a Navigation Controller. Is there a way to programmatically (in iOS5+) jump (push/pop) to different views? For instance, I have

NavigationController->RootViewController->DetailViewController1->DetailViewController2->DetailViewController3

How can I, say, jump from RootViewController to DetailViewController3, and then jump back to DetailViewController1?

Thanks.


Solution

  • You can try something like this.

    1. Get your VCs from storyboard. (You have to set identifiers in storyboard)

      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
      UIViewController *DetailViewController1 = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController1Identifier"];
      UIViewController *DetailViewController3 = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController3Identifier"];
      
    2. Push VCs way you need.

      [self.navigationController pushViewController:(DetailViewController1) animated:YES];
      [self.navigationController pushViewController:(DetailViewController3) animated:YES];
      

    Now, when you press "back" you'll see DetailViewController1.