我有一个导航控制器堆栈,比方说A(Root)-> B ->C->D。现在我想单击D中的一个按钮,直接弹出B。引用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]我不明白原因,因为错误信息中的数字"3“和"2”没有任何关系。
额外的信息,以防你需要它们:
希望你能帮我!谢谢!
发布于 2014-09-12 07:23:51
for (UIViewController *controllers in self.navigationController.viewControllers) {
if ([controllers isKindOfClass:[DesiredViewController class]]) {
[self.navigationController popToViewController:controllers
animated:YES];
break;
}
}发布于 2014-09-12 10:06:47
使用以下代码:
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];
}
} 发布于 2014-09-12 07:55:20
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实例。
https://stackoverflow.com/questions/25802754
复制相似问题