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

前端 未结 13 1035
独厮守ぢ
独厮守ぢ 2020-12-02 17:04

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

MyInboxVC *inboxVC=[MyInboxVC get ];
//upload all the pending messag         


        
相关标签:
13条回答
  • 2020-12-02 17:17

    [devNavController pushViewController:inboxVC animated:NO]; Set animated as NO

    0 讨论(0)
  • 2020-12-02 17:19

    It means you are pushing the same viewcontroller object to stack again when it's already in there.

    [self.navigationController pushViewController:viewControllerObj animated:NO];
    
    [self.navigationController pushViewController:viewControllerObj animated:NO];
    

    check if u r pushing inside a loop or if u've accidentally placed the code more than one time..

    0 讨论(0)
  • 2020-12-02 17:20

    The Main Reason for this problem, obviously if the code that pushed the view controller is called more than once. This could occur for many reasons, most common mistake when a callback method is triggered from a background thread, where this method could be executed more than once while it is still pushing the view controller. Example: Calling a service api on background thread when tapping a button, which will allow you to press the button more than once, and therefore the callback which pushes the view controller get called more than once. @Melvin and @Sam solution is valid as long as you do not want to fix the original problem by not pushing more than once.

    0 讨论(0)
  • 2020-12-02 17:23

    Make sure you are not adding the view controller twice in the navigation stack. Eg - in below example self.mainViewC is pushed twice because it is initially instantiated in the navController, and is then pushed onto the navController again in the last line, which would cause this issue.

      navController=[[UINavigationController alloc] initWithRootViewController:self.mainViewC];  
      self.window.rootViewController = navController;
      [self.window makeKeyAndVisible];        
      [navController pushViewController:self.mainViewC animated:NO]; 
    

    In this case mainViewC has already been added to stack when initWithRootViewController was written. There is no need of pushViewController again.

    0 讨论(0)
  • 2020-12-02 17:27

    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]]) {
    
    0 讨论(0)
  • 2020-12-02 17:31

    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");
      }
    }
    
    0 讨论(0)
提交回复
热议问题