2

App crashes with this error

NSInvalidArgumentException :Pushing the same view controller instance more than once is not supported 

Getting this error when trying to push another navigationcontroller from another navigation controller's UIBarButtonItem.

Message shows in console:

UINavigationController pushViewController:transition:forceImmediate:]_block_invoke + 0

This is the segue coding

else if ([segue.identifier isEqualToString:@"showGroupView"]) {
        GroupView *groupView  = (GroupView *)segue.destinationViewController;

        [self.navigationController pushViewController:groupView animated:YES];
}

If anyone can help resolve this error

user1120133
  • 3,244
  • 3
  • 48
  • 90
  • 1
    If you're using a segue, you shouldn't also be pushing in code. Do one or the other, not both. – rdelmar Jan 16 '15 at 22:32

4 Answers4

1

The error message tells you exactly what the problem is: you're attempting to push a view controller instance which is already on the stack.

In other words, segue.destinationViewController has already been pushed, such that it is either one of the parents of the current view controller, or the current controller itself.

The actual reason for this is impossible to determine without seeing your code. It's possible that you've got a similar problem to this question, in that the runtime is allowing the event to occur twice. It's equally possible that something to do with the segue is off.

As a first step, I would suggest adding log statements to the segue (not breakpoints, as they will change the observable behaviour) in order to see if it is being called more than once (and is thus similar to the linked question).

Community
  • 1
  • 1
sapi
  • 9,944
  • 8
  • 41
  • 71
1

Well this i how i checked if my view controller is on the navigation stack or not but that actually solved my problem

if ([[self.navigationController topViewController] isKindOfClass:[groupView class]]){
          self.navigationController.navigationBarHidden = YES;
      }else{
          self.navigationController.navigationBarHidden = NO;
      }

So the whole code goes like this for anyone reference though

else if ([segue.identifier isEqualToString:@"showGroupView"]) {
          GroupView *groupView  = (GroupView *)segue.destinationViewController;
      if ([[self.navigationController topViewController] isKindOfClass:[groupView class]]){
          self.navigationController.navigationBarHidden = YES;
      }else{
          self.navigationController.navigationBarHidden = NO;
      }
      NSLog(@"showGroupViewsegued");

  }
user1120133
  • 3,244
  • 3
  • 48
  • 90
0

For your suitison ,you should remove "[self.navigationController pushViewController:groupView animated:YES];", and it will jump to the storyBpard of the specific segue.destionViewController.

I test it myself.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// 只作为传值使用 不用再进行导航控制器的 跳转操作

// No need for navigation to push or show

if ([segue.destinationViewController isKindOfClass:[AddCategory class]]) {
    AddCategory *controller = (AddCategory *)segue.destinationViewController;
    controller.categoryText = sender;
 }
 if ([segue.identifier isEqualToString:@"AddCategory"]) {
    //进入到下一步骤 :只作为传值使用
//specilized

}else if ([segue.identifier isEqualToString:@"AddCategory2"]){

 }

}

Matiji66
  • 709
  • 7
  • 14
0

In my case the first push was not fast enough, so I run into the same problem. Fix was to check the situation:

if (targetViewController == self.navigationController?.topViewController) {
  NSLog("ignore double")
} else {
  self.navigationController!.pushViewController(targetViewController, animated: true)
}
info-gerd
  • 1
  • 2