iPhone Development:Pop to particular viewController


In this post i am going to tell you use of how one can pop back to previous view-controller in iPhone SDK.
Objective C provides us two ways to pop back to a previous view-controller placed in a navigation stack.They are two ways to do it

1. popToViewController:(UIViewController *) animated:(BOOL)
2. popViewControllerAnimated:(BOOL)
Second one will navigate back you to the top view-controller of navigation stack, in other words this will take you back to last view-controller visited by you.
e.g. Your app contains 5 screens and your flow is 
Screen 1 –> Screen 2 —> Screen 3 –> Screen 4 —> Screen 5
Now currently you are on Screen 5, if you use popViewControllerAnimated method then you will be redirected back to Screen 4 since you visited screen 4 before coming to Screen5.
You can use this method as
[self.navigationController popViewControllerAnimated:YES];

Now, if you want to pop back to Screen 2 and currently you are on Screen 5. To achieve this first you have to iterate all viewController placed in navigationController Stack and then you can use  first method  given above.
Below is the piece of code that explains you , how you can achieve this scenario
 for (UIViewController *vC in [self.navigationController viewControllers])
    {
      if ([vC isKindOfClass:[StoreScreen class]])
        {
            [self.navigationController popToViewController:vC animated:YES];
        }
    }
NOTE:- StoreScreen is name of ViewController.

For documentation... UINavigationController Documentation