Tutorial :NavigationController becomes nil after popToViewController



Question:

My navigationcontroller becomes nil after my "[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:0] animated:YES];"

this is my scene: InsertViewController - >

[self.navigationController pushViewController:choiceViewController animated:YES];

ChoiceViewController ->

 [self.navigationController pushViewController:choiceDetailViewController animated:YES];

ChoiceDetailViewController ->

  InsertViewController *insertViewController =  [self.navigationController.viewControllers objectAtIndex:0] ;  UINavigationController *secondaryNavigationCtrl = [[UINavigationController alloc] initWithRootViewController:insertViewController];  secondaryNavigationCtrl.navigationBar.barStyle = UIBarStyleBlackOpaque;  [self presentModalViewController:secondaryNavigationCtrl animated:YES];  [secondaryNavigationCtrl release];  [

When "ok" button pressed( self.navigationItem.leftBarButtonItem) in InsertView that just poped up, then it goes back to ChoiceDetailViewController and i do a [code][self.navigationController dismissModalViewControllerAnimated:YES]; [/code]

After that i do a

  [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:0] animated:YES];  
Which go back tot the InsertViewController, and when i do the cycle again i see my navigationcontroller is nil...

Any idea what I am doing wrong?

Thanks in advance.


Solution:1

I'm not sure I've understand what you are trying to do but, when you go back to ChoicheDetailViewController i think you should do a [self.navigationController popViewController:...] instead of [self.navigationController dismissModalViewControllerAnimated:YES] because ChoicheDetailViewController got pushed on the stack in a non modal way in the first place. So with your code you are actually dismissing the whole navigationController.


Solution:2

Not sure if this fixes your problem but I had this problem because the class that implemented the code that popped the view controller was actually getting popped. This caused my self.navigationController to be nil because it itself was getting removed. I moved the code to a class that was not getting popped and it did not get set to nil.

Before the stack looked like this, one of the ViewControllers gets popped self.navigationController is nil.

PopableViewController     functionThatCallsPopToViewController  PopableViewController     functionThatCallsPopToViewController  RootViewController  

After

PopableViewController  PopableViewController  RootViewController     functionThatCallsPopToViewController  

Since RootViewController is not popped self.navigationController did not get set to nil. The only tricky thing is, now you need to keep a reference to the RootViewController in your other viewControllers.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »