【问题标题】:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“试图弹出到不存在的视图控制器。”
【发布时间】:2019-08-11 13:43:49
【问题描述】:

我遇到了这个没有被捕获的异常,即使是在处理异常(@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


    【解决方案1】:

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

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

    【讨论】:

    • 我尝试了这个解决方案,但没有奏效,因为 if 条件不适用于这种情况,因为 rootViewControler 不是 topViewController.navigationController 的子项。
    【解决方案2】:

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

    [(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 之后立即在 A 内调用其子 A.child 已被删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 2012-07-28
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多