开发者

will my uinavigationcontroller be retained or will it be released

navigationC开发者_运维问答ontroller = [[[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 owner of 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 else retained 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_ACCESS for 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.


addSubview: retains navigationController.view. Removing it from the parent releases it. Since you autoreleased it, you don't need to retain it or release it - it'll be handled for you.


No, the view controller does not get released, because the view controller's view is a property of the view controller. The view controller's view is not the same object as the view controller. You are not adding the view controller to the parent view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜