0

Possible Duplicate:
iphone - “Pushing the same view controller instance more than once is not supported” exception

I am properly pushing test2ViewController from 1 as follows,

self.controller2 = [[test2ViewController alloc] initWithNibName:@"test2ViewController" bundle:nil anUser:self.idUser];

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

[self.controller2 release];

from 2 to 1 I pop it after initialize 1 again (necessary to initialize).

self.controller1 = [[test1ViewController alloc] initWithNibName:@"test1ViewController" bundle:nil anUser:self.idUser];

    [[self navigationController]  popToRootViewControllerAnimated:NO];

    [self.controller1 release];

and problem appers when trying to push again 2 from 1, app crashes with an error,

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

what am doing wrong? thank you.

Community
  • 1
  • 1
Jaume
  • 3,672
  • 19
  • 60
  • 119

3 Answers3

1

Well first off you are creating another instance of test2ViewController so you will go to a different instance each time you change view.

What you should do:

if(!test2ViewController)
    secondView = [[test2ViewController alloc] init...];
[self navigationController pushViewController:secondView animated:NO];

and to return, simply:

[self.navigationController popViewControllerAnimated:NO];

PoppingtoRoot causes you to pop to the very first view controller that used the pushViewController method.

PappaSmalls
  • 349
  • 2
  • 13
0

Judging by the code you posted you only push one view controller (controller2) to your navigation controller.

popToRootViewControllerAnimated: will remove all view controllers from the stack except the root view controller (which seems to be controller2 in your case). So basically it does nothing.

Then you try to push the same view controller2 again and it fails, because as the error message said, that's not allowed.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
0

You do not need to reinitialize viewController1 again; if you are pushing the viewController2 from 1, than you only have to invoke

[self.navigationController popToRootViewControllerAnimated:NO];

because viewController1 is already in the stack. What this method will do is to remove all viewControllers on the stack except the first one and move back to it.

If viewController1 is not the rootView controller you should use

[self.navigationController popViewControllerAnimated:NO];

which will pop only the last pushed viewcontroller on the stack and reveal the one beneath it.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56