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

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

我有这个异常没有被捕获,即使是在处理异常中(@try{}@catch{}),这可能很容易,但我现在看不到。异常显示 '试图弹出一个不存在的视图控制器。' 我相信正在传递一个参数 nil 但我没有看到它:

-(void) theProblemMethod
{

    dispatch_async(dispatch_get_main_queue(), ^{
        @try {
                [[self topViewController] dismissViewControllerAnimated:YES completion: ^{

                     UIViewController * rootViewControler = nil;
                    if ((rootViewControler = (UIViewController *) [UIApplication sharedApplication].keyWindow.rootViewController))
                     {
                         if([self topViewController])
                             [(UINavigationController *)[self topViewController].navigationController popToViewController:rootViewControler animated:YES];
                         if ((rootViewControler = (UIViewController *) [[[[UIApplication sharedApplication] delegate] window] rootViewController].presentedViewController)) {
                             [rootViewControler dismissViewControllerAnimated:YES completion:
                              ^{
                                  //do something here
                              }];
                         }
                     }
                 }];

        } @catch (NSException *exception) {
            NSLog(@"There is a problem at [myClass theProblemMethod]  Exception: %@, reason: %@",  [exception name], [exception reason]);
        } @finally {}
     });
}

有人看到问题了吗?

当弹出视图控制器为零,或者弹出视图控制器不在导航视图控制器堆栈中时,会发生此错误。在弹出之前检查两者。

UIViewController *poppedVC = ...
UINavigationController *nc = ...
if (poppedVC && [nc.viewControllers containsObject:poppedVC]) {
    [nc popViewControllerAnimated:poppedVC];
}

我发现了问题!我刚刚发现问题指向了这一行:

[(UINavigationController *)[self topViewController].navigationController popToViewController:rootViewControler animated:YES];

我的代码试图在关闭 topViewController 视图(其父视图)后访问 属性 navigationController

这个问题的解决方案是在 @try 之后关闭 topViewController 之前将 navigationControllerstrong text 存储在时间变量中:

UINavigationController * aNavigationController = (UINavigationController *)[[self topViewController] navigationController];

最后:

 -(void) theProblemMethod
    {

        dispatch_async(dispatch_get_main_queue(), ^{
            @try {
                    UINavigationController * aNavigationController = (UINavigationController *)[[self topViewController] navigationController];
                    [[self topViewController] dismissViewControllerAnimated:YES completion: ^{

                         UIViewController * rootViewControler = nil;
                        if ((rootViewControler = (UIViewController *) [UIApplication sharedApplication].keyWindow.rootViewController))
                         {
                                 [(UINavigationController *)[self topViewController].navigationController popToViewController:rootViewControler animated:YES];
                             if ((rootViewControler = (UIViewController *) [[[[UIApplication sharedApplication] delegate] window] rootViewController].presentedViewController)) {
                                 [rootViewControler dismissViewControllerAnimated:YES completion:
                                  ^{
                                      //do something here
                                  }];
                             }
                         }
                     }];

            } @catch (NSException *exception) {
                NSLog(@"There is a problem at [myClass theProblemMethod]  Exception: %@, reason: %@",  [exception name], [exception reason]);
            } @finally {}
         });
    }

基本上我是在删除 A 并同时尝试在 中调用它的子 A.child ]A 就在 A 被删除之后。