Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing View Controller more than once is not supported?

I am getting the following error whenever I try to switch views like this:

-(void)changeView1ToView4 {
    [self.navigationController1 pushViewController:view4 animated:YES];
}

This doesn't happen when the app first loads and user goes directly to this view. This crash only happens when I go to one of my other views, come back to the main menu, and then try to go to this view.

Also if you weren't sure already, I am using a UINavigationController. Also this code is in the app delegate and I am calling it from a view controller which has a parent view so I am using a .reference to call it like this:

[self.reference changeView1ToView4];

Is there any real way to fix this?

Thanks!

Edit1:

[self.navigationController1 pushViewController:view4 animated:NO];
[self.navigationController1 pushViewController:view4 animated:YES];

I tried that and got this crash message in the console:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<View2: 0x18d540>)'
like image 435
SimplyKiwi Avatar asked Sep 11 '11 18:09

SimplyKiwi


People also ask

Can any Uiviewcontroller push another?

Yes, you can push or present another view controller without any action. You just need to call the push or present code where you want to trigger it.

Can we dismiss view controller if pushed?

If you are using pushviewcontroller method then to dismiss you have to use popviewcontroller method .


2 Answers

When pushing 2 views onto the stack, try to call:

[self.navigationController1 pushViewController:view4 animated:YES];

and

[self.navigationController1 pushViewController://secondviewcontrollername// animated:NO];

If you try to push more than one view with the animated: field set to YES on both, then you confuse the stack. Only animate one view at a time.

like image 200
CodaFi Avatar answered Oct 10 '22 18:10

CodaFi


just an FYI, if you call setViewControllers:animated: there's no need to call pushViewController: afterwards, else you'll get the "Pushing the same view controller instance more than once is not supported" crash.

like image 21
Cole Avatar answered Oct 10 '22 18:10

Cole