ios 如何从导航控制器一次弹出两个视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8236940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:32:58  来源:igfitidea点击:

How do I pop two views at once from a navigation controller?

iosiphoneuinavigationcontroller

提问by Adam Waite

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?

但是我怎么一次做两个呢?

回答by Meet

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

回答by budidino

Here are two UINavigationControllerextensions that can solve your problem. I would recommend using the first one that pops to a UIViewControllerof a specific class:

这里有两个UINavigationController扩展可以解决您的问题。我建议使用弹出到UIViewController特定类的第一个:

extension UINavigationController {

  func popToViewController(ofClass: AnyClass, animated: Bool = true) {
    if let vc = viewControllers.filter({
// pop to SomeViewController class
navigationController?.popToViewController(ofClass: SomeViewController.self)

// pop 2 view controllers
navigationController?.popViewControllers(viewsToPop: 2)
.isKind(of: ofClass)}).last { popToViewController(vc, animated: animated) } } func popViewControllers(viewsToPop: Int, animated: Bool = true) { if viewControllers.count > viewsToPop { let vc = viewControllers[viewControllers.count - viewsToPop - 1] popToViewController(vc, animated: animated) } } }

and use it like this:

并像这样使用它:

[self.navigationController popToRootViewControllerAnimated:YES];

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

回答by chown

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

您可以使用以下命令弹出“根”(第一个)视图控制器popToRootViewControllerAnimated

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];   

UINavigationControllerReference:

UINavigationController参考

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.

弹出堆栈上除根视图控制器之外的所有视图控制器并更新显示。

返回值
从堆栈中弹出的视图控制器数组。

回答by Zaraki

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")
        }
    }
}

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

objectAtIndex:1 --> 你可以传递你想要弹出的任何索引

回答by lubilis

Swift 2 - xCode 7.3

斯威夫特 2 - xCode 7.3

This could be a very useful extension to pop UIViewControllers:

这可能是弹出 UIViewControllers 的一个非常有用的扩展:

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

- (void) popTwoViewControllersAnimated:(BOOL)animated;

@end

回答by Tieme

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:

如果您只想一次弹出 2 因为您的 rootViewController 是(方式)“更深”,那么 2 您可以考虑向 UIviewController 添加一个类别,例如:

UINavigationController+popTwice.h

UINavigationController+popTwice.h

#import "UINavigationController+popTwice.h"

@implementation UINavigationController (popTwice)

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

@end

UINavigationController+popTwice.m

UINavigationController+popTwice.m

[self.navigationController popTwoViewControllersAnimated:YES];

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

#import "UINavigationController+popTwice.h"在您的实现中的某处导入类别,并使用这行代码一次弹出 2 个控制器:

func popViewControllerss(popViews: Int, animated: Bool = true) {
    if self.navigationController!.viewControllers.count > popViews
    {
        let vc = self.navigationController!.viewControllers[self.navigationController!.viewControllers.count - popViews - 1]
         self.navigationController?.popToViewController(vc, animated: animated)
    }
}

isn't that great? :)

那不是很好吗?:)

回答by Saifan Nadaf

Swift 4 :

斯威夫特 4:

self.popViewControllerss(popViews: 2)

Then Use This Method

然后用这个方法

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

回答by Sabareesh

[self.navigationController popToViewController:yourViewController animated:YES];

回答by Leena

You can also try this one :-

你也可以试试这个:-

    NSMutableArray *newStack = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
    [newStack removeLastObject];
    [newStack removeLastObject];
    [self.navigationController setViewControllers:newStack animated:YES];

回答by patrick-fitzgerald

##代码##