Shi*_*ari 5 uiviewcontroller uinavigationcontroller ios poptoviewcontroller
嗨,我正在开发一个我应该去的应用程序:
UIViewController1到UIViewController2UIViewController2到UIViewController3UIViewController3到UIViewController4UIViewController4回到UIViewController2我在用UINavigationController.当我使用[self.navigationController pushViewController:VC2 animated:NO];,[self.navigationController popViewControllerAnimated:NO];一切正常.
但是当我使用[self.navigationController popToViewController:VC2 animated:NO];从UIViewController4应用程序终止说法Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'
以下是我的代码如何弹出UIViewController2
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewController2 class]]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewController2 *VC2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController popToViewController:VC2 animated:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
当我打印导航阵列时,它显示UIViewController2个堆栈.我已经UINavigationController从Editor-> embed in-> Navigation Controller添加了
谁能告诉我为什么会这样?我试图搜索这个问题,但没有任何帮助
Oli*_*erM 18
在这里,您实例化一个新的VC2视图控制器实例,这不是导航视图控制器堆栈上的实例!
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewController2 *VC2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
Run Code Online (Sandbox Code Playgroud)
所以你必须找到正确的实例
[[self.navigationController] viewControllers]
Run Code Online (Sandbox Code Playgroud)
解决方案1 :(正如iDev所说,跳转到堆栈上的第二个视图控制器,如果你知道它是第二个,请使用它)
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
Run Code Online (Sandbox Code Playgroud)
解决方案2 :(通常在堆栈上返回2级)
NSUInteger ownIndex = [self.navigationController.viewControllers indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:ownIndex - 2] animated:YES];
Run Code Online (Sandbox Code Playgroud)
那是你的代码:
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewController2 class]]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewController2 *VC2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController popToViewController:VC2 animated:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
你可能已经发现的vc是一种ViewController2,那就是已经在导航堆栈.新实例,您创建为VC2的ViewController2,是不是在导航栈,它永远不会是.
看到例外的正文:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试弹出到不存在的视图控制器.
因此,您想要导航回未进入导航堆栈的视图控制器 - 这会导致崩溃.
你必须导航回已经在堆栈中的视图控制器- 这是你的vc:
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewController2 class]]) {
[self.navigationController popToViewController:vc animated:NO];
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用此行将viewcontroller弹出到第2行
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9633 次 |
| 最近记录: |