我有一个导航控制器堆栈,让我们说A(根) - > B-> C-> C-> C-> C-> D.现在我想点击D中的一个按钮直接弹出我。参考Stackoverflow的一些解决方案,我用过:
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
但它会抛出NSRangeException:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
我不明白这个原因,因为与#34; 3"无关。和" 2"在错误消息中。
如果您需要,请提供额外信息:
答案 0 :(得分:1)
for (UIViewController *controllers in self.navigationController.viewControllers) {
if ([controllers isKindOfClass:[DesiredViewController class]]) {
[self.navigationController popToViewController:controllers
animated:YES];
break;
}
}
答案 1 :(得分:1)
使用此代码:
Let view controllers in stack are A->B->C->D->E
如果你想弹出C然后
for(UIViewController *vc in [self.navigationController.viewControllers])
{
if(vc isKindOfClass [C class])
{
[self.navigationController popToViewController:vc animated:YES];
}
}
答案 2 :(得分:0)
NSArray *existingControllers = [self.navigationController viewControllers];
B *theB;
for(UIViewController *eachCtrl in existingControllers)
{
if([eachCtrl isKindOfClass:[B class]])
{
int Bx = [existingControllers indexOfObject:eachCtrl]
theB = [existingControllers objectAtIndex:Bx];
}
}
//Now do your popping of B controller
if(nil != theB)
[self.navigationController popToViewController:theB animated:YES];
我认为你犯了很多'C'实例的错误。 并且还在验证时验证existingControllers是否包含您的B实例。
答案 3 :(得分:0)
使用此:
if ([[self.navigationController viewControllers] count]>1) {
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
}
答案 4 :(得分:0)
使用时实际上没有错:
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
如果导航堆栈是A(根) - > B-> C-> C-> C-> C-> D。我已为此测试并附加了一个示例项目。