我有一个导航堆栈,用比如5个UIViewControllers。 我想删除第三和第四viewcontrollers在堆栈上的一个按钮,在第5的ViewController点击。 是否有可能做到这一点? 如果是的话怎么样?
Answer 1:
使用此代码,并享受:
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
// [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.
[navigationArray removeObjectAtIndex: 2]; // You can pass your index here
self.navigationController.viewControllers = navigationArray;
[navigationArray release];
希望这会帮助你。
编辑:Swift代码
var navigationArray = self.navigationController?.viewControllers //To get all UIViewController stack as Array
navigationArray!.remove(at: (navigationArray?.count)! - 2) // To remove previous UIViewController
self.navigationController?.viewControllers = navigationArray!
Answer 2:
你可以先拿到阵列中的所有视图控制器,然后用相应的视图控制器类检查后,你可以删除你想要的。
下面是小块代码:
NSArray* tempVCA = [self.navigationController viewControllers];
for(UIViewController *tempVC in tempVCA)
{
if([tempVC isKindOfClass:[urViewControllerClass class]])
{
[tempVC removeFromParentViewController];
}
}
我想,这会让你的工作更轻松。
Answer 3:
迅速3&4
self.navigationController!.viewControllers.removeAll()
self.navigationController?.viewControllers.remove(at: "insert here a number")
雨燕2.1
移除所有:
self.navigationController!.viewControllers.removeAll()
删除索引
self.navigationController?.viewControllers.removeAtIndex("insert here a number")
有一堆像removeFirst,范围等更可能采取的行动
Answer 4:
雨燕2.0:
var navArray:Array = (self.navigationController?.viewControllers)!
navArray.removeAtIndex(navArray.count-2)
self.navigationController?.viewControllers = navArray
Answer 5:
如果你试图移动到距离第五视图控制器第二个视图控制器(跳过第3和第4),你想用[self.navigationController popToviewController:secondViewController]
你可以得到secondViewController
从导航控制器堆栈。
secondViewController = [self.navigationController.viewControllers objectAtIndex:yourViewControllerIndex];
Answer 6:
使用setViewControllers
从功能UINavigationController
是最好的方式。 还有animated
参数启用动画。
func setViewControllers(_ viewControllers: [UIViewController], animated: Bool)
例如,在迅速的问题
func goToFifthVC() {
var currentVCStack = self.navigationController?.viewControllers
currentVCStack?.removeSubrange(2...3)
let fifthVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "fifthVC")
currentVCStack?.append(fifthVC)
self.navigationController?.setViewControllers(currentVCStack!, animated: true)
}
我试过其他类似的方法[tempVC removeFromParentViewController];
。 它使怪异的行为,删除视图控制器导航仍呈现时弹出回来就好报道@罗宾ellerkmann
Answer 7:
用这个
if let navVCsCount = navigationController?.viewControllers.count {
navigationController?.viewControllers.removeSubrange(Range(2..<navVCsCount - 1))
}
这将需要navigationController的ViewControllers的照顾。 viewControllers并且还层叠的导航栏一个navigationItems。
注意: 确保至少viewDidAppear之后调用它
Answer 8:
斯威夫特5:
navigationController?.viewControllers.removeAll(where: { (vc) -> Bool in
if vc.isKind(of: MyViewController.self) || vc.isKind(of: MyViewController2.self) {
return false
}
else {
return true
}
})
Answer 9:
该解决方案在迅速4为我工作:
let VCCount = self.navigationController!.viewControllers.count
self.navigationController?.viewControllers.removeSubrange(Range(VCCount-3..<VCCount - 1))
在堆栈当前视图控制器的指标为:
self.navigationController!.viewControllers.count - 1