navigationController = [[[UINavigationController alloc] initWithRootViewController:firstMenuView] autorelease];
[view addSubview:navigationController.view];
Does view keep a retain on navigtioncontroller? Or do I need to retain it?
Does calling navcontroller.view removefromparent actually release the nav controller?
The controller is the
ownerof the view, the view will keep a weak reference (non-retained) to the controller, the controller will keep a strong (retained) reference to the view. In this case, your navigationController has been autoreleased, so it should theoretically end up deallocated, as long as nobody elseretained it.The navigationController’s view on the other hand has been added as a subview to another, which means it is retained by the parent view. This is a problem, because if the view has any calls to the controller, those calls will go to a deallocated object.
99% of the time you get
EXC_BAD_ACCESSfor this, and your app crashes. The other 1% is much worse.Fortunately though, all you need to do to prevent the above mentioned disasters is find something to “own” your navigation controller, give it a @property (retain) and assign your navigation controller to that. (Keep the autorelease, that part is good)
Good candidates to own your navigation controller are the view controller of the parent view, and the application delegate.
This way, the controller will stick around as long as it is needed, because it will have been retained at least once.
What’s important here is the distinction between the controller and the view. The view object is retained, the controller object is alloc’d then autoreleased, so it’s gone unless you retain it.