Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Pushing the same view controller instance more than once is not supported" exception

I am using the following code to retrieve some messages and putting them into my inbox.

MyInboxVC *inboxVC=[MyInboxVC get ];
//upload all the pending messages
UINavigationController *devNavController=[[MyappMgr get]getDeveloperNavigationController ];

[devNavController pushViewController:inboxVC animated:YES];
[devNavController setNavigationBarHidden:NO];

I get the exception

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<MyInboxVC: 0x1452a0>)'

What does it mean? What am I doing wrong?

like image 923
Suchi Avatar asked Aug 16 '11 18:08

Suchi


3 Answers

I believe when you do some actions really fast this can happens too. I build something in like this:

if(![self.navigationController.topViewController isKindOfClass:[YOURCLASS class]]) {
like image 136
Melvin Avatar answered Nov 15 '22 07:11

Melvin


Firstly handle the crash so it doesnt kill your app:

@try {
    [self.navController pushViewController:viewController animated:NO];
} @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
} @finally {
    //NSLog(@"finally");
}

Then if you get the error use popTo

- (void)pushViewController:(UIViewController *)viewController {
  if (viewController) {
    @try {
        [self.navController pushViewController:viewController animated:NO];
    } @catch (NSException * ex) {
        //“Pushing the same view controller instance more than once is not supported” 
        //NSInvalidArgumentException
        NSLog(@"Exception: [%@]:%@",[ex  class], ex );
        NSLog(@"ex.name:'%@'", ex.name);
        NSLog(@"ex.reason:'%@'", ex.reason);
        //Full error includes class pointer address so only care if it starts with this error
        NSRange range = [ex.reason rangeOfString:@"Pushing the same view controller instance more than once is not supported"];

        if ([ex.name isEqualToString:@"NSInvalidArgumentException"] &&
           range.location != NSNotFound) {
            //view controller already exists in the stack - just pop back to it
            [self.navController popToViewController:viewController animated:NO];
        } else {
            NSLog(@"ERROR:UNHANDLED EXCEPTION TYPE:%@", ex);
        }
    } @finally {
        //NSLog(@"finally");
    }
  } else {
    NSLog(@"ERROR:pushViewController: viewController is nil");
  }
}
like image 23
brian.clear Avatar answered Nov 15 '22 06:11

brian.clear


It means that the ViewController returned from [MyInboxVC get] is already in the navigation stack of devNavController. You can not add the same object to the stack multiple times.

Apparently, you already have a MyInboxVC pushed earlier. Insure that you've popped it when it was no longer needed.

That's the "what's it mean" answer, but don't have enough info to know what you need to do to fix it.

My guess is your Navigation Stack is growing larger than you are expecting, meaning you are not popping as often as you should.

like image 8
MarkPowell Avatar answered Nov 15 '22 06:11

MarkPowell