5

嗨,我正在开发一个我应该去的应用程序:

  1. UIViewController1 到UIViewController2
  2. UIViewController2 到UIViewController3
  3. UIViewController3 到UIViewController4
  4. UIViewController4 回到UIViewController2

我正在使用UINavigationController. 当我使用时[self.navigationController pushViewController:VC2 animated:NO];[self.navigationController popViewControllerAnimated:NO];一切正常。

但是当我使用[self.navigationController popToViewController:VC2 animated:NO];from 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];
            }
        }

当我打印导航数组时,它UIViewController在堆栈中显示 2。我UINavigationController从编辑器->嵌入->导航控制器中添加了

谁能告诉我为什么会这样?我试图搜索这个问题,但没有任何帮助

4

3 回答 3

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];
于 2014-08-05T10:01:03.030 回答
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];
    }
}
于 2014-08-05T10:06:21.883 回答
5

试试这条线来弹出你的 viewcontroller 到 2 nd

  [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
于 2014-08-05T09:39:56.753 回答