Search code examples
iosobjective-cuistoryboarduistoryboardsegue

UINavigationController pop to another root controller


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.


Solution

  • 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.