I have a method in which I alloc and init an
UIView (`tabsClippedView = [[[UIView alloc] initWithFrame:tabsClippedFrame] autorelease];`).
This view has another view added to it
(`tabsView = [[[UIView alloc] initWithFrame:tabsFrame] autorelease];`).
Then I initiate a couple of buttons
(e.g. `UIButton* btn = [[[UIButton alloc] initWithFrame:frame] autorelease];`)
and add them to the subview of the view.
Now from time to time, I need to delete all the buttons and assign them again. Is the best way to delete the entire view or simply the subview to which I added the buttons?
How would I need to do this (without memory leaking etc.)? Would a simple
self.tabsView = nil;
suffice to delete the view and all of its subviews (i.e. the buttons)?
Or would it better to delete the superview as well, to start entirely from scratch:
self.tabsClippedView = nil;
Another solution: don't remove your
UIButtons
but re-use them. Don't know the exact use-case for you, but you may assigntags
to yourUIButtons
and find them with[UIView viewWithTag:]
.later...
Besides: In my opinion you only should use
autorelease
when you've no other options. Here it's easy for you to releasebtn
after adding it as subview.Since your UIView is autoreleased, you just need to remove it from the superview. For which there is the
removeFromSuperview
method.So, you'd just want to call
[self.tabsView removeFromSuperview]
. As long as your property declaration is set toretain
that's all you'll need.