Quote:
Originally Posted by Shubo
When you remove a view from its superview using "removeFromSuperview" does it remove the view's subview as well or do you have to remove the subviews yourself before removing the view from its superview?
Thanks.
|
The answer is "it depends."
The view you remove from it's superview won't remove ITs views, but it might release them. Here's how it works.
If you remove a view from it's superview, the superview sends the newly removed view a release message. If that causes it's retain count to drop to zero, it gets deallocated. When a view is deallocated, it releases it's subviews also.
If you want to have a view (and all it's subviews) get deallocated when you remove it from it from it's superview, make sure the only object retaining it is it's superview. Then, when you remove it, the whole ball of wax gets deallocated.
If you want the view and it's subviews to stick around so you can use it somewhere else, have your view controller save it in a retained property.
This is a very common design pattern in Cocoa. You have some object that serves as a container for other objects. (views, arrays, dictionaries, sets, and custom data container objects, and even complex groups like an array of dictionaries of arrays of objects) Only the container object retains the things it contains. When you release the container object, everything "inside" the container gets released.