Pedro Trujillo
Pedro Trujillo

Reputation: 1691

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

I'm having this exception that is not being caught, even is in a handling exception (@try{}@catch{}), it is probably something very easy but I can't see it at the moment. The exception says 'Tried to pop to a view controller that doesn't exist.' I believe a parameter is being pass nil but I don't see it:

-(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 {}
     });
}

Does anyone see the problem?

Upvotes: 0

Views: 834

Answers (2)

Pedro Trujillo
Pedro Trujillo

Reputation: 1691

I found the problem! I just found that the problem pointed to be at the line:

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

My code was trying to access the property navigationController after dismiss topViewController view (its parent).

The solution for this was store the navigationControllerstrong text in a temporal variable before dismiss topViewController after the @try:

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

Finally :

 -(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 {}
         });
    }

Basically I was removing A and trying at the same time to invoke its child A.child inside A right after A was removed.

Upvotes: 0

Silence
Silence

Reputation: 416

This error happens when the popped view controller is nil, or the popped view controller is not in the navigation view controller stack. Check both before popping.

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

Upvotes: 1

Related Questions