[英]How do I pop two views at once from a navigation controller?
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?但是我怎么一次做两个呢?
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];
}
}
Here are two UINavigationController
extensions that can solve your problem.这里有两个
UINavigationController
扩展可以解决您的问题。 I would recommend using the first one that pops to a UIViewController
of a specific class:我建议使用第一个弹出到特定类的
UIViewController
的:
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.filter({$0.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:并像这样使用它:
// pop to SomeViewController class
navigationController?.popToViewController(ofClass: SomeViewController.self)
// pop 2 view controllers
navigationController?.popViewControllers(viewsToPop: 2)
You can pop to the "root" (first) view controller with popToRootViewControllerAnimated
:您可以使用
popToRootViewControllerAnimated
弹出到“根”(第一个)视图控制器:
[self.navigationController popToRootViewControllerAnimated:YES];
// If you want to know what view controllers were popd:
// NSArray *popdViewControllers = [self.navigationController popToRootViewControllerAnimated:YES];
UINavigationController
Reference : 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.从堆栈中弹出的视图控制器数组。
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
objectAtIndex:1 --> 你可以传递你想要弹出的任何索引
Swift 2 - xCode 7.3斯威夫特 2 - xCode 7.3
This could be a very useful extension to pop UIViewControllers:这可能是流行 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")
}
}
}
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 添加一个类别,例如:
#import <UIKit/UIKit.h>
@interface UINavigationController (popTwice)
- (void) popTwoViewControllersAnimated:(BOOL)animated;
@end
#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:在您的实现中的某处导入类别
#import "UINavigationController+popTwice.h"
并使用这行代码一次弹出 2 个控制器:
[self.navigationController popTwoViewControllersAnimated:YES];
isn't that great?那不是很好吗? :)
:)
Swift 4 :
斯威夫特 4:
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)
}
}
Then Use This Method然后用这个方法
self.popViewControllerss(popViews: 2)
//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];
你也可以试试这个:-
[self.navigationController popToViewController:yourViewController animated:YES];
NSMutableArray *newStack = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
[newStack removeLastObject];
[newStack removeLastObject];
[self.navigationController setViewControllers:newStack animated:YES];
In Swift 3 , you can pop one, two or more viewcontrollers from navigation controller like that在 Swift 3 中,您可以像这样从导航控制器中弹出一个、两个或多个视图控制器
let viewControllers = self.navigationController!.viewControllers as [UIViewController]
for aViewController:UIViewController in viewControllers {
if aViewController.isKind(of: FromWhereYouWantToGoController.self) {
_ = self.navigationController?.popToViewController(aViewController, animated: true)
}
}
Here FromWhereYouWantToGoController is destination controller.这里 FromWhereYouWantToGoController 是目标控制器。 Hope it helps.
希望能帮助到你。
You can pass the initial view controller (the one you want to come back to) and then call this line in the last view:您可以传递初始视图控制器(您想要返回的视图控制器),然后在最后一个视图中调用此行:
[self.navigationController popToViewController:yourInitialViewController animated:YES];
L. L。
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:
如果您只想弹出一些(不是一直到根目录),您可以通过 self.navigationController.viewControllers 检查类类型进行迭代,或者如果您有一个参考使用:
for (UIViewController *aViewController in self.navigationController.viewControllers) {
if ([aViewController isKindOfClass:[SMThumbnailViewController class]]) {
[self.navigationController popToViewController:aViewController animated:YES];
}
}
Here's a little function I'm using to go back X ViewControllers:这是我用来返回 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];
}
}
Swift Version as of (Swift 1.2 / Xcode 6.3.1) of popping twice or more Swift 版本 (Swift 1.2 / Xcode 6.3.1) 弹出两次或更多次
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)
you can pop back to the root view controller你可以弹回根视图控制器
[self.navigationController popToRootViewControllerAnimated:YES];
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或者,如果要弹出的视图不是第一个,则需要在上一个视图的 viewWillAppear 中再次弹出
You can use the stack of the UIViewControllers.您可以使用 UIViewControllers 的堆栈。 1. Fetch the array of all the viewControllers in the stack.
1. 获取栈中所有 viewController 的数组。 2. Iterate through the whole array and find the desired viewController
2.遍历整个数组,找到想要的viewController
by matching the class type.通过匹配类类型。 3. Pop the viewController.
3. 弹出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)
}
}
}
Using a simple UINavigationController extension :使用一个简单的UINavigationController 扩展:
extension UINavigationController {
func popViewControllers(_ count: Int) {
guard viewControllers.count > count else { return }
popToViewController(viewControllers[viewControllers.count - count - 1], animated: true)
}
}
Swift 4.2 Swift 4.2
On your navigationController?.viewControllers
you can check your viewControllers on your navigation controller, so then you just need to pop the position you want to, just like that: 在您的
navigationController?.viewControllers
您可以检查导航控制器上的viewControllers,这样您只需弹出您想要的位置,就像这样:
navigationController?.popToViewController(navigationController?.viewControllers[1], animated: true)
Where 1 is the position of my ViewController that I want to pop out. 其中1是我想要弹出的ViewController的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.