Ser Pounce
Ser Pounce

Reputation: 14553

UIView Delete Itself?

I have a UIView subclass called ToolbarView that sort of acts like a floating window and has a UIToolbar that has an "X" button that can make the window disappear. My question is, is it possible for the object to delete itself from within its own class?

For instance, within ToolbarView.m, I have a method closeButtonPushed which fires when the X button is pushed. Is it as simple as to remove the view from its superview, and then call dealloc? Is it even necessary to call dealloc as once it is removed from the superview there won't be any other pointers to it. Or is this bad memory practice?

Upvotes: 1

Views: 1553

Answers (2)

rakeshNS
rakeshNS

Reputation: 4257

What you have to do is in closeButtonPushed method is, call

 [self removeFromSuperView];

it will remove the ToolbarView object and IOS will call the dealloc method and release objects. No need to call dealloc manually.

Upvotes: 0

D.C.
D.C.

Reputation: 15588

Yeah, don't call dealloc ever. If the superview is the only object that has a reference to your view, removing the view will cause its retain count to go to 0, and the runtime will then deallocate the object for you.

Upvotes: 4

Related Questions