Reputation: 8384
I have a view hierarchy that I need to remove entirely. Is it okay to just call removeFromSuperview
on the top parent view or do I need to visit each child node recursively and remove it individually?
Edit: Just to clarify, I understand that removing the parent physically removes the children from view, but does that also decrease their ref-counts appropriately?
Upvotes: 7
Views: 2066
Reputation: 9198
No, removing a view doesn't do anything to it's child views (but of course they will no longer be drawn as they aren't in the view hierarchy any more).
Of course, if you remove a view and have no other strong references to it, it may be deallocated at some point in the future (although you shouldn't care about this or assume that there are no other references). When and if it is deallocated it will remove it's child views and release them.
Upvotes: 2
Reputation: 22939
Yes. You can think of views as a tree structure. So if you tell a view to be removed from the super view, the whole structure will be removed.
For example: A UIButton
is actually a view which contains an UILabel
which displays the title of the button. So you can do this [myUIButton removeFromSuperview];
, which will remove the button and its own view hierarchy (including the button's containing UILabel).
Remark: If you only want to hide/and show a view you can also set its hidden
property to YES
or NO
instead of removing the view from the view hierarchy. Like this it is easy to display the view again.
Upvotes: 3
Reputation: 6413
All subviews belong to the view; so, when you remove a view from it's superview - it is removed with all it's subviews.
Upvotes: 6