开发者

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

开发者 https://www.devze.com 2023-03-17 13:59 出处:网络
In my app I\'m implementing UINavigationController. There are several UIViewControllers that are being pushed in the stack开发者_C百科.

In my app I'm implementing UINavigationController. There are several UIViewControllers that are being pushed in the stack开发者_C百科.

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

精彩评论

暂无评论...
验证码 换一张
取 消