How to correctly remove view from it's superview if no longer needed?

2.8k views Asked by At

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 before I call that, I should call setHidden:YES? Would that be enough?

2

There are 2 answers

3
e.James On BEST ANSWER

That warning is there so that you don't call removeFromSuperview from within a drawRect: method. The Cocoa runtime makes extensive use of the view hierarchy during drawing operations, so removing a view from its superview while drawing can really screw things up.

Calling removeFromSuperview at any other time is perfectly fine, and there is no need to hide the view prior to removing it.

0
marcc On

Wow. I've never seen that note in the docs before and I just got a little scared about some code I've written :)

http://www.iphonedevsdk.com/forum/iphone-sdk-development/9729-curious-thing-removefromsuperview-doc.html

The consensus is that it's a poorly worded sentence and you should not invoke this method while in the process of displaying/drawing something. But if it's currently displayed, then it's ok.

I'd really recommend asking Apple for guidance on this one though.