How to pop back to specify viewController from navigationController(viewControllers/stack)?

12,415

Solution 1

Here i would like to know you that NavigationController manage the ViewController by the Concept Of Stack.ie LAST COME FIRST OUT.Here are two Approach for doing the Same.

1)Here you can make iteration for getting the desirable ViewController as Below

  for (id controller in [self.navigationController viewControllers])
   {
   if ([controller isKindOfClass:[BViewController class]])
  {
    [self.navigationController popToViewController:controller animated:YES];
    break;
  }
  }

2) Here you have A,B,C,D Controllers. means B would be on 3rd Position so what can you do

you can hard wired the Index as Below

  [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];

I hope it may helpFull to you..

Solution 2

You could probably do something like:

for (UIViewController *controller in [self.navigationController viewControllers])
{
    if ([controller isKindOfClass:[B class]])
    {
        [self.navigationController popToViewController:controller animated:YES];
        break;
    }
}

Update:

In iOS 6, you can just use unwind segues, which takes care of all of this for you. Just go to your B class, define an unwind action (identified as such by the combination of the IBAction return type and the UIStoryboardSegue parameter), e.g.:

- (IBAction)done:(UIStoryboardSegue *)segue
{
    // do any clean up you want
}

Then, any controller that's presented by B can then can create an unwind segue by, for example, control-dragging from a button to the exit icon in the scene's dock. You can optionally have the controller that you're unwinding from do the expected prepareForSegue (which is called before B's unwind action).

Share:
12,415
Lothario
Author by

Lothario

Updated on June 05, 2022

Comments

  • Lothario
    Lothario almost 2 years

    I've created an application with serial views (A,B,C,D,...), and I need to pop back D to B. Someone may say Why not using:

    [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
    

    However, it is not a good solution. Because this method needs you to get the index which our "B" is store in.

    Question: How to get the index of "B" in the viewControllers?

    format in the UIViewControllers should be:
    "< A: 0x6e70710 >", "< C: 0x6e30370 >", "< B: 0x6988a70 >", "< D: 0x6ea8950 >", "< E: 0x6eaaad0 >"

    I've tried and failed to use rangeOfString and hasPrefix to get the "B" view's index.