简体   繁体   中英

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

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM