我想弹出导航堆栈上的第三个视图回到第一个视图。
我知道如何一次弹出一个视图:
[self.navigationController popViewControllerAnimated:YES];
但是我该如何一次做两个?
Answers:
您也可以尝试在导航控制器堆栈之间跳转
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
if ([aViewController isKindOfClass:[RequiredViewController class]]) {
[self.navigationController popToViewController:aViewController animated:NO];
}
}
这里有两个UINavigationController
扩展可以解决您的问题。我建议使用第一个弹出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)
}
}
}
并像这样使用它:
// pop to SomeViewController class
navigationController?.popToViewController(ofClass: SomeViewController.self)
// pop 2 view controllers
navigationController?.popViewControllers(viewsToPop: 2)
removeLast
just removeLast(2)
。
let vc = viewControllers[viewControllers.count - viewsToPop + 1]
,为了正常工作,您需要将其替换为:let vc = viewControllers[viewControllers.count - (viewsToPop + 1)]
或let vc = viewControllers[viewControllers.count - viewsToPop - 1]
您可以使用以下命令弹出到“根”(第一个)视图控制器popToRootViewControllerAnimated
:
[self.navigationController popToRootViewControllerAnimated:YES];
// If you want to know what view controllers were popd:
// NSArray *popdViewControllers = [self.navigationController popToRootViewControllerAnimated:YES];
弹出堆栈中除根视图控制器之外的所有视图控制器,并更新显示。
返回值
从堆栈中弹出的视图控制器数组。
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
objectAtIndex:1->您可以传递要弹出的任何索引
Swift 2-xCode 7.3
这对于弹出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")
}
}
}
如果您只是想一次弹出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 "UINavigationController+popTwice.h"
您的实现中的某处,并使用此行代码立即弹出2个控制器:
[self.navigationController popTwoViewControllersAnimated:YES];
那不是很好吗?:)
斯威夫特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)
}
}
然后使用此方法
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];
NSMutableArray *newStack = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
[newStack removeLastObject];
[newStack removeLastObject];
[self.navigationController setViewControllers:newStack animated:YES];
在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)
}
}
这里FromWhereYouWantToGoController是目标控制器。希望能帮助到你。
我在这里没有看到这个答案。如果您只想弹出一些(不是一直弹出到根目录),则可以通过self.navigationController.viewControllers进行迭代以检查类类型,或者如果您有引用,请使用:
for (UIViewController *aViewController in self.navigationController.viewControllers) {
if ([aViewController isKindOfClass:[SMThumbnailViewController class]]) {
[self.navigationController popToViewController:aViewController animated:YES];
}
}
这是我用来返回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 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.获取堆栈中所有viewControllers的数组。2.遍历整个数组,并
通过匹配类类型找到所需的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)
}
}
}
使用一个简单的UINavigationController扩展:
extension UINavigationController {
func popViewControllers(_ count: Int) {
guard viewControllers.count > count else { return }
popToViewController(viewControllers[viewControllers.count - count - 1], animated: true)
}
}