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:35
    NSMutableArray *newStack = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
    [newStack removeLastObject];
    [newStack removeLastObject];
    [self.navigationController setViewControllers:newStack animated:YES];
查看更多
Explosion°爆炸
3楼-- · 2019-01-16 03:38

You can pop to the "root" (first) view controller with popToRootViewControllerAnimated:

[self.navigationController popToRootViewControllerAnimated:YES];

// If you want to know what view controllers were popd:
// NSArray *popdViewControllers = [self.navigationController popToRootViewControllerAnimated:YES];

UINavigationController Reference:

Pops all the view controllers on the stack except the root view controller and updates the display.

Return Value
An array of view controllers that are popped from the stack.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-16 03:38

Swift Version as of (Swift 1.2 / Xcode 6.3.1) of popping twice or more

 var viewControllers = self.navigationController?.viewControllers
 viewControllers?.removeLast()
 viewControllers?.removeLast()
 self.navigationController?.setViewControllers(viewControllers, animated: true)

or

 var viewControllers = self.navigationController?.viewControllers
 var viewsToPop = 2
 var end = (viewControllers?.count)!
 var start = end - viewsToPop
 viewControllers?.removeRange(Range<Int>(start: start, end: end))
 self.navigationController?.setViewControllers(viewControllers, animated: true)
查看更多
家丑人穷心不美
5楼-- · 2019-01-16 03:38

You can use the stack of the UIViewControllers. 1. Fetch the array of all the viewControllers in the stack. 2. Iterate through the whole array and find the desired viewController
by matching the class type. 3. Pop the viewController.

func popToSpecificViewC
{
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
        for viewController:UIViewController in viewControllers
        {
            if viewController.isKind(of: WelcomeViewC.self)
            {
                _ = self.navigationController?.popToViewController(viewController, animated: true)
            }
        }
}
查看更多
登录 后发表回答