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

Ios ***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“试图弹出到不存在的视图控制器。”,ios,objective-c,xcode,cocoa-touch,uikit,Ios,Objective C,Xcode,Cocoa Touch,Uikit,我有一个没有被捕获的异常,即使是在处理异常@try{}@catch{},这可能是非常简单的事情,但我现在看不到它。异常显示“试图弹出到不存在的视图控制器”。我相信某个参数是pass nil,但我没有看到它: -(void) theProblemMethod { dispatch_async(dispatch_get_main_queue(), ^{ @try { [[self topViewController] dismissViewC

我有一个没有被捕获的异常,即使是在处理异常@try{}@catch{},这可能是非常简单的事情,但我现在看不到它。异常显示“试图弹出到不存在的视图控制器”。我相信某个参数是pass 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:

最后:

 -(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被移除之后

我尝试了此解决方案,但没有成功,因为if条件不适用于此情况,因为rootViewController不是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 {}
         });
    }