如何从导航控制器一次弹出两个视图?
我想将导航堆栈上的第三个视图弹回到第一个视图。
我知道如何一次弹出一个视图:
[self.navigationController popViewControllerAnimated:YES];
但是如何同时弹出两个视图呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您也可以尝试在导航控制器堆栈之间跳转
You can try this to Jump between the navigation controller stack as well
这里有两个
UINavigationController
扩展可以解决您的问题。我建议使用第一个弹出到特定类的UIViewController
的:并像这样使用它:
Here are two
UINavigationController
extensions that can solve your problem. I would recommend using the first one that pops to aUIViewController
of a specific class:and use it like this:
您可以使用 popToRootViewControllerAnimated 弹出到“根”(第一个)视图控制器:
UINavigationController
参考:You can pop to the "root" (first) view controller with
popToRootViewControllerAnimated
:UINavigationController
Reference:objectAtIndex:1 -->你可以传递你想要弹出的任何索引
objectAtIndex:1 --> you can pass whichever index you want to pop to
Swift 2 - xCode 7.3
这可能是弹出 UIViewControllers 的一个非常有用的扩展:
Swift 2 - xCode 7.3
This could be a very useful extension to pop UIViewControllers:
如果您只想一次弹出 2 个,因为您的 rootViewController(方式)“更深”,那么 2 您可以考虑向 UIviewController 添加一个类别,例如:
UINavigationController+popTwice.h
UINavigationController+popTwice.m
导入类别
#import “UINavigationController+popTwice.h”
在您的实现中的某个位置,并使用这行代码一次弹出 2 个控制器:这不是很好吗? :)
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
UINavigationController+popTwice.m
Import the category
#import "UINavigationController+popTwice.h"
somewhere in your implementation and use this line of code to pop 2 controllers at once:isn't that great? :)
然后使用此方法
Then Use This Method
您也可以尝试这个:-
You can also try this one :-
在 Swift 3 中,
您可以从导航控制器中弹出一个、两个或多个视图控制器,例如
此处 FromWhereYouWantToGoController 是目标控制器。希望有帮助。
In Swift 3,
you can pop one, two or more viewcontrollers from navigation controller like that
Here FromWhereYouWantToGoController is destination controller. Hope it helps.
您可以传递初始视图控制器(您想要返回的视图控制器),然后在最后一个视图中调用此行:
L。
You can pass the initial view controller (the one you want to come back to) and then call this line in the last view:
L.
我在这里没有看到这个答案。如果您只想弹出一些(不是一直到根),您可以迭代 self.navigationController.viewControllers 检查类类型,或者如果您有引用,请使用:
I didn't see this answer in here. If you only want to pop a few (not all the way to the root), you can just iterate through self.navigationController.viewControllers checking for class types, or if you have a reference use that:
您可以弹回到根视图控制器
,或者,如果您要弹到的视图不是第一个视图,则需要在上一个视图的 viewWillAppear 中再次弹起
you can pop back to the root view controller
or, if the view you want to pop to isn't the first one, you'll need to pop again in your previous view's viewWillAppear
这是我用来返回 X ViewControllers 的一个小函数:
Here's a little function I'm using to go back X ViewControllers:
Swift 版本(Swift 1.2 / Xcode 6.3.1)
弹出两次或更多次
或
Swift Version as of (Swift 1.2 / Xcode 6.3.1)
of popping twice or more
or
您可以使用 UIViewController 的堆栈。
1. 获取堆栈中所有viewController 的数组。
2. 遍历整个数组并找到想要的viewController
通过匹配类类型。
3.弹出viewController。
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.
使用简单的 UINavigationController 扩展:
Using a simple UINavigationController extension: