Search code examples
uiviewuiviewcontrollerreleaseaddsubview

UIView: adding UIViewController's view as a subview and its removing


I would like to ask what is the correct way to add and remove UIViewController's view as a child view. So, having UIViewController initialized I can add its view to view hierarchy as follows:

UIViewController *myViewControler = [[UIViewController alloc] init];
[someAnotherView addSubview:myViewController.view];

Question 1: Should I release myViewController.view after addSubview: call?

If I want to remove myViewController's view from view hierarchy I call [myViewController.view removeFromSuperview];

Question 2: How should I release myViewController instance in this case after its view removedFromSuperview?


Solution

    1. You do not need to release the view, the owning view controller will do this for you.

    2. I normally put the declaration of myViewController in the header and then release and nil it when I am done with it (either somewhere in the normal flow or in the dealloc of the containing view controller).