Pushing View Controller more than once is not supported?
Asked Answered
A

4

5

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>)'
Acquah answered 11/9, 2011 at 18:22 Comment(1)
I'm not really sure what your question is talking about, but yes, it's true that you cannot push the same view controller more than once—UINavigationController will throw an exception. Nothing you can do about that.Asphaltite
P
10

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.

Praenomen answered 16/9, 2011 at 1:37 Comment(6)
Yes. Put the view controller that will be pushed first (obviously the non-animated one) up top, then put the animated one on the bottom. The stack will call the view controller on top, then the view controller on the bottom and the bottom one should be the view that you see. Tell me if it isn't.Praenomen
Check Edit1 in my original postAcquah
AH! I thought you were trying to push 2 DIFFERENT views at once. Why would you need to push a view twice? Just push the view once by deleting one of those methods. Your app should work perfectly.Praenomen
you mean by just using one of those lines? Thats what I did originally and it is causing this crash.Acquah
I see what you're doing wrong! In your method that pushes a viewcontroller, you have to define view4. Like this for example: firstview *first = [[firstview alloc]initiwithnibname:@"firstview" bundle:nil] then release it after the view is pushed:Praenomen
In my situation I was calling two segues one after the other. I changed the first to a push with no animation. Works fine now.Insalubrious
K
4

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.

Kylander answered 6/3, 2012 at 1:10 Comment(0)
E
0
            @try {
               [self.navController pushViewController:viewController animated:NO];
            } @catch (NSException * e) {
                NSLog(@"Exception: %@", e);
                 [self.navigationController popToViewController:viewController animated:NO];
            } @finally {
                //NSLog(@"finally");
            }
Equip answered 29/6, 2016 at 21:39 Comment(0)
M
0

Check this condition before push

if (![[self.navigationController topViewController] isKindOfClass:[YOURCONTROLLER class]])
    [self.navigationController pushViewController:YOURCONTROLLER animated:YES];
Marlie answered 3/6, 2017 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.