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

时间:2019-03-20 18:37:30

标签: ios objective-c xcode cocoa-touch uikit

我有一个未被捕获的异常,即使是在处理异常(@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 {}
     });
}

有人看到这个问题吗?

2 个答案:

答案 0 :(得分:1)

当弹出的视图控制器为nil或弹出的视图控制器不在导航视图控制器堆栈中时,会发生此错误。弹出前先检查一下。

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

答案 1 :(得分:0)

我发现了问题!我只是发现问题出在下面:

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

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

解决方案是将NavigationController strong文本存储在时间变量中,然后在@try之后关闭 topViewController

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之后在 A 中调用其子级 A.child 已删除。