两次调用 removeFromSuperview 会导致崩溃或副作用 [英] will calling removeFromSuperview twice cause a crash or side effect
问题描述
我有一个 UIView 元素,我将它添加到主视图控制器中.
I have a UIView element which I am adding to the main view controller.
由于我的代码的设计,removeFromSuperview 在我的代码的不同位置被调用了两次.
Due to the design of my code, it so happens that the removeFromSuperview is called twice in different places of my code.
[myview removeFromSuperview];
这就是我的代码,所以我想知道调用removeFromSuperview"是否会导致任何问题.
That is how my code is, so I want to know if calling the 'removeFromSuperview' causes any problem.
或者如何检查视图是否在超级视图中并且仅将其删除.
Or how do I check if the view is in the superview and only the remove it.
例如
if (myview in superview)
[myview removeFromSuperview];
else
do nothing
推荐答案
removeFromSuperview
的文档说明如下:
如果接收者的superview不为nil,则superview释放接收者.如果您打算重用视图,请务必在之前保留它调用此方法,稍后根据需要再次释放.
If the receiver’s superview is not nil, the superview releases the receiver. If you plan to reuse a view, be sure to retain it before calling this method and release it again later as appropriate.
这意味着不应该发生崩溃或副作用,并且您所询问的检查已经由该方法的实现执行了.
It means that no crash or side effect should happen, and a check you're asking about is already performed by the implementation of this method.
但是,如果您需要检查您的视图是否作为子视图添加到其他视图中,您可以使用以下代码:
However, if you need to check if your view is added as subview to some other view, you can use the following code:
if( theView.superview != nil )
{
// theView is a subview for some view
}
这篇关于两次调用 removeFromSuperview 会导致崩溃或副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!