How to "pop" several View controllers in UINavigationController Stack?

You can try the popToRootViewControllerAnimated:, popToViewController:animated: and popViewControllerAnimated: messages of the UINavigationController class.


In your case it is really usefull to use popToRootViewcontrollerAnimated: as suggested by Irene, but if somebody need to pop exact number of controllers, then following code can be usefull:

- (void) popControllersNumber:(int)number
{
    if (number <= 1)
        [[self navigationController] popViewControllerAnimated:YES];
    else
    {
        NSArray* controller = [[self navigationController] viewControllers];
        int requiredIndex = [controller count] - number - 1;
        if (requiredIndex < 0) requiredIndex = 0;
        UIViewController* requireController = [[[self navigationController] viewControllers] objectAtIndex:requiredIndex];
        [[self navigationController] popToViewController:requireController animated:YES];
    }
}

Use

 TravelViewController *travelView = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3];
 [self.navigationController popToViewController:travelView animated:YES];