0

I have a normal view controller and I want to add a uinavigationcontroller to it so:


[self.view addSubview:aNavigationController.view];
everything works, fine, aNavigationController is an IBOutlet, in the XIB, it's view controller is loaded from another xib, then in the navigation controller's view controller's class I type this:

- (IBAction)anAction {
[self.navigationController pushViewController:aViewController animated:YES];
}
everything works fine, the view changes to the aViewController view and it's animated, but when I type in aViewController's class this:

- (IBAction)anotherAction {
[self.navigationController popViewControllerAnimated:YES];
}
it crashes, why?
Skullhouseapps
  • 498
  • 1
  • 7
  • 14

1 Answers1

2

Because there is no view to pop. When you're trying to pop view controller it is expected that there is some view in stack, i.e. the view from which you calling popViewControllerAnimated was already pushed earlier.

So popping is not just awesome animation but navigation through stack of views in navigation controller. In this particular situation you're trying to call -1st element of this stack, that is the reason of the crash.

Dig deeper here:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH103-SW1

knuku
  • 6,082
  • 1
  • 35
  • 43
  • but if I pushed aViewController why doesn't this mean i added it to the navigation stack – Skullhouseapps Feb 23 '11 at 10:27
  • Yes, you're adding it. But as I see you're calling `popViewControllerAnimated` from your root controller (it is the *first* element of `UINavigationController` stack) but not from the controller you've pushed (it is the *second* element of the stack). That is the mistake. You'd never call `popViewControllerAnimated` from the root controller. – knuku Feb 23 '11 at 10:47