Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly removeFromSuperview?

Apple says:

removeFromSuperview Unlinks the receiver from its superview and its window, and removes it from the responder chain.

  • (void)removeFromSuperview

Never invoke this method while displaying.

So, when I want to get rid of a view, I was used to just kick it off from it's superview. Why should I never invoke that while it's visible? So I must set it to hidden=YES before I do that?

like image 965
Thanks Avatar asked Jul 17 '09 16:07

Thanks


People also ask

Does Remove from superview remove constraints?

If the view's superview is not nil , the superview releases the view. Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.


1 Answers

The specification specifies "while displaying" not "while it is on display". Thus, you should never call removeFromSuperview in the view's drawRect for example.

removeFromSuperview releases the view and may deallocate it. The parent view when attempting to display the view may not expected it to be deallocated and cause a corrupted access.

like image 72
notnoop Avatar answered Oct 19 '22 09:10

notnoop