How do I pop two views at once from a navigation c

2019-01-16 03:07发布

I want to pop to the third view on the navigation stack back to the first view.

I know how to pop one view at once:

[self.navigationController popViewControllerAnimated:YES];

But how do I do two at once?

Thanks...

16条回答
聊天终结者
2楼-- · 2019-01-16 03:11

You can try this to Jump between the navigation controller stack as well

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
    if ([aViewController isKindOfClass:[RequiredViewController class]]) {
        [self.navigationController popToViewController:aViewController animated:NO];
    }
}
查看更多
冷血范
3楼-- · 2019-01-16 03:14
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];   

objectAtIndex:1 --> you can pass whichever index you want to pop to

查看更多
乱世女痞
4楼-- · 2019-01-16 03:15
//viewIndex = 1;
//viewIndex = 2;
//viewIndex = 3;

// **[viewControllers objectAtIndex: *put here your viewindex number*]

NSArray *viewControllers = [self.navigationController viewControllers];
[self.navigationController popToViewController:[viewControllers objectAtIndex:viewIndex] animated:NO];
查看更多
\"骚年 ilove
5楼-- · 2019-01-16 03:16

Swift 2 - xCode 7.3

This could be a very useful extension to pop UIViewControllers:

extension UINavigationController {

    func popToViewControllerOfType(classForCoder: AnyClass) {
        for controller in viewControllers {
            if controller.classForCoder == classForCoder {
                popToViewController(controller, animated: true)
                break
            }
        }
    }

    func popViewControllers(controllersToPop: Int, animated: Bool) {
        if viewControllers.count > controllersToPop {
            popToViewController(viewControllers[viewControllers.count - (controllersToPop + 1)], animated: animated)
        } else {
            print("Trying to pop \(controllersToPop) view controllers but navigation controller contains only \(viewControllers.count) controllers in stack")
        }
    }
}
查看更多
男人必须洒脱
6楼-- · 2019-01-16 03:16

If you just want to pop 2 at once because your rootViewController is (way) 'deeper' then 2 you could consider adding a category to UIviewController for example:

UINavigationController+popTwice.h

#import <UIKit/UIKit.h>
@interface UINavigationController (popTwice)

- (void) popTwoViewControllersAnimated:(BOOL)animated;

@end

UINavigationController+popTwice.m

#import "UINavigationController+popTwice.h"

@implementation UINavigationController (popTwice)

- (void) popTwoViewControllersAnimated:(BOOL)animated{
    [self popViewControllerAnimated:NO];
    [self popViewControllerAnimated:animated];
}

@end

Import the category #import "UINavigationController+popTwice.h" somewhere in your implementation and use this line of code to pop 2 controllers at once:

[self.navigationController popTwoViewControllersAnimated:YES];

isn't that great? :)

查看更多
聊天终结者
7楼-- · 2019-01-16 03:16

You can also try this one :-

[self.navigationController popToViewController:yourViewController animated:YES];
查看更多
登录 后发表回答