[self.navigationController popToViewController:VC2 animated:NO];紧急

时间:2014-08-05 09:34:35

标签: ios uiviewcontroller uinavigationcontroller poptoviewcontroller

您好我正在开发一个我应该去的应用程序:

  1. UIViewController 1到UIViewController 2
  2. UIViewController 2到UIViewController 3
  3. UIViewController 3到UIViewController 4
  4. UIViewController 4返回UIViewController 2
  5. 我正在使用UINavigationController。当我使用[self.navigationController pushViewController:VC2 animated:NO];[self.navigationController popViewControllerAnimated:NO];时,一切正常。

    但是当我使用来自[self.navigationController popToViewController:VC2 animated:NO];的{​​{1}}时,4个应用程序终止UIViewController

    以下是我的代码我如何弹出Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.' 2

    UIViewController

    当我打印导航阵列时,它会在堆栈中显示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]; } } 2。 我添加了编辑器中的UIViewController>嵌入 - >导航控制器

    任何人都可以告诉我为什么会这样吗?我试图搜索这个问题,但没有任何帮助

3 个答案:

答案 0 :(得分:18)

在这里,您实例化一个新的VC2视图控制器实例,这不是导航视图控制器堆栈上的实例!

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
            ViewController2 *VC2 = [storyboard     instantiateViewControllerWithIdentifier:@"ViewController2"];

所以你必须在

中找到正确的实例
[[self.navigationController] viewControllers]

解决方案1 ​​:(正如iDev所说,跳转到堆栈上的第二个视图控制器,如果你知道它是第二个,请使用它)

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

解决方案2 :(通常在堆栈上返回2级)

NSUInteger ownIndex = [self.navigationController.viewControllers indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:ownIndex - 2] animated:YES];

答案 1 :(得分:9)

这是你的代码:

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];
    }
}

您可能已经发现vc是一种ViewController2,而且已经在导航堆栈中。您创建为VC2 ViewController2的新实例在导航堆栈中从未

查看异常的正文:

  

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试弹出到不存在的视图控制器。'

因此,您想要导航回未导入导航堆栈的视图控制器 - 这会导致崩溃。

你必须导航回一个已经已经在堆栈中的视图控制器 - 这是你的vc

for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[ViewController2 class]]) {
        [self.navigationController popToViewController:vc animated:NO];
    }
}

答案 2 :(得分:5)

尝试使用此行将viewcontroller弹出到第二个

  [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];