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

问题描述 投票:0回答:2

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

有谁看到这个问题?

ios objective-c xcode cocoa-touch uikit
2个回答
1
投票

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

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

0
投票

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

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

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

对此的解决方案是将navigationControllerstrong文本存储在时间变量中,然后在@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。

© www.soinside.com 2019 - 2024. All rights reserved.