简体   繁体   English

如果不再需要如何从其超级视图中正确删除视图?

[英]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. removeFromSuperview取消接收者与其超级视图及其窗口的链接,并将其从响应者链中删除。

  • (void)removeFromSuperview (空隙)removeFromSuperview

Never invoke this method while displaying. 在显示时切勿调用此方法。

So before I call that, I should call setHidden:YES? 所以在我打电话之前,我应该打电话给setHidden:YES? Would that be enough? 够了吗?

That warning is there so that you don't call removeFromSuperview from within a drawRect: method. 该警告在那里,所以您不要从drawRect:方法中调用removeFromSuperview 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. 在任何其他时间调用removeFromSuperview都很好,并且在删除视图之前无需隐藏视图。

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 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. 我确实建议您向苹果寻求有关此方面的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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