如何从导航控制器一次popup两个视图?
我想popup到导航堆栈上的第三个视图,回到第一个视图。
我知道如何一次popup一个视图:
[self.navigationController popViewControllerAnimated:YES];
但我怎么一次做两个?
谢谢…
您也可以尝试在导航控制器堆栈之间跳转
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]]; for (UIViewController *aViewController in allViewControllers) { if ([aViewController isKindOfClass:[RequiredViewController class]]) { [self.navigationController popToViewController:aViewController animated:NO]; } }
你可以使用popToRootViewControllerAnimated
popup到“root”(第一个)视图控制器:
[self.navigationController popToRootViewControllerAnimated:YES]; // If you want to know what view controllers were popd: // NSArray *popdViewControllers = [self.navigationController popToRootViewControllerAnimated:YES];
UINavigationController
参考 :
popup除根视图控制器外的所有视图控制器,并更新显示。
返回值
从堆栈popup的视图控制器数组。
你可以尝试这样的事情:
迅速
var viewControllers = navigationController?.viewControllers viewControllers?.removeLast(2) // views to pop navigationController?.setViewControllers(viewControllers!, animated: true)
目标C
int viewsToPop = 2; [self.navigationController popToViewController: self.navigationController.viewControllers[self.navigationController.viewControllers.count-viewsToPop-1] animated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
objectAtIndex:1 – >你可以传递你要popup的任何索引
如果你只是想立即popup2,因为你的rootViewController(方法)'更深',那么2你可以考虑添加一个类别到UIviewController例如:
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 "UINavigationController+popTwice.h"
导入#import "UINavigationController+popTwice.h"
,并使用以下代码行一次popup2个控制器:
[self.navigationController popTwoViewControllersAnimated:YES];
不是很好吗? 🙂
Swift 2 – xCode 7.3
这可能是一个非常有用的扩展来popupUIViewControllers:
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") } } }
你也可以试试这个:
[self.navigationController popToViewController:yourViewController animated:YES];
//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];
NSMutableArray *newStack = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]]; [newStack removeLastObject]; [newStack removeLastObject]; [self.navigationController setViewControllers:newStack animated:YES];
您可以传递初始视图控制器(您想要返回的控制器),然后在最后一个视图中调用此行:
[self.navigationController popToViewController:yourInitialViewController animated:YES];
L.
这里有一个我用来返回X ViewControllers的函数:
-(void)backMultiple:(id)data { int back = 2; //Default to go back 2 int count = [self.navigationController.viewControllers count]; if(data[@"count"]) back = [data[@"count"] intValue]; //If we want to go back more than those that actually exist, just go to the root if(back+1 > count) { [self.navigationController popToRootViewControllerAnimated:YES]; } //Otherwise go back X ViewControllers else { [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:count-(back+1)] animated:YES]; } }
你可以回弹到根视图控制器
[self.navigationController popToRootViewControllerAnimated:YES];
或者,如果要popup的视图不是第一个视图,则需要在先前的视图的viewWillAppear中再次popup
我在这里没有看到这个答案。 如果你只想popup几个(不是全部到根目录),你可以迭代self.navigationController.viewControllers检查类的types,或者如果你有一个引用使用:
for (UIViewController *aViewController in self.navigationController.viewControllers) { if ([aViewController isKindOfClass:[SMThumbnailViewController class]]) { [self.navigationController popToViewController:aViewController animated:YES]; } }
(Swift 1.2 / Xcode 6.3.1)的两次或更多的Swift版本
var viewControllers = self.navigationController?.viewControllers viewControllers?.removeLast() viewControllers?.removeLast() self.navigationController?.setViewControllers(viewControllers, animated: true)
要么
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)
你可以使用UIViewControllers的堆栈。 1.获取堆栈中所有viewController的数组。 遍历整个数组,find所需的viewController
通过匹配类的types。 3.popupviewController。
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) } } }
在Swift 3中 ,可以像这样从导航控制器中popup一个,两个或多个视图控制器
let viewControllers = self.navigationController!.viewControllers as [UIViewController] for aViewController:UIViewController in viewControllers { if aViewController.isKind(of: FromWhereYouWantToGoController.self) { _ = self.navigationController?.popToViewController(aViewController, animated: true) } }
这里FromWhereYouWantToGoController是目标控制器。 希望能帮助到你。