2
votes

I have 3 View Controllers. The first 2 view controllers are embedded in UINavigationController, as shown in the screenshot:

Storyboard screenshot

The starting point is at View Controller 1's UINavigationController. Then, I push to View Controller 3. However, I would like it to pop to the root of UINavigationController containing View Controller 2. How can I change the navigation hierarchy?

  • need to keep the popping animation
  • i know changing root view controller is a way to achieve, but this will be very odd in user experience

p.s. this storyboard is simplified for illustration purpose; they are many view controllers in between View Controller 1, 2 and 3.

4

4 Answers

1
votes

I end up changing the entire UINavigationController hierarchy with the following codes:

// In the View Controller 3's button's `IBAction`:
NSArray *vcs = @[[self.storyboard instantiateViewControllerWithIdentifier:@"NavController1_Identifier"], self];
[self.navigationController setViewControllers:vcs];
[self.navigationController popToRootViewControllerAnimated:YES];

where NavController1_Identifier is the first UINavigationController's storyboard identifier.


Seems unwind segue can be used in this case too.

0
votes

You need to change your rootViewController. You can pop to any view Controller only if it is in the same navigation stack.

0
votes

Since the second view controller you want to go to is not in your navigation stack, you can't pop to it. What you can do is without loosing the smooth trasition animation is:

1: push to view controller 3

var contr = self.storyboard!.instantiateViewControllerWithIdentifier("3") as! ViewController3
self.navigationController?.pushViewController(contr, animated: true)

2: push to view controller 2 from view controller 3, you can use the same code as above, just make sure to change the identifier of the view controller.

3: you can now pop from here back to the original screen you started from:

self.navigationController?.popToRootViewControllerAnimated(true)

Yet if you want to go to view controller 2 without pushing, you can just present the view controller, this will start the navigation stack at view controller 2, however loosing the previous views.

0
votes

change root view controller as below..

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];