简体   繁体   中英

How to “pop” several View controllers in UINavigationController Stack?

In my app I'm implementing UINavigationController . There are several UIViewControllers that are being pushed in the stack.

When I reach the last one, I wish to have (upon a user action) all the UIViewControllers be popped except for the first UIViewController . How do I do that?

I understand how to pop the last one, but how do I instruct all the previous ones to disappear as well?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM