ios 删除所有子视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2156015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:50:16  来源:igfitidea点击:

Remove all subviews?

iossubview

提问by Ian Vink

When my app gets back to its root view controller, in the viewDidAppear:method I need to remove all subviews.

当我的应用程序返回到其根视图控制器时,在该viewDidAppear:方法中我需要删除所有子视图。

How can I do this?

我怎样才能做到这一点?

回答by e.James

Edit:With thanks to cocoafan: This situation is muddled up by the fact that NSViewand UIViewhandle things differently. For NSView(desktop Mac development only), you can simply use the following:

编辑:感谢cocoafan:这种情况因处理事情的方式不同NSView而变得混乱UIView。对于NSView(仅限桌面 Mac 开发),您可以简单地使用以下内容:

[someNSView setSubviews:[NSArray array]];

For UIView(iOS development only), you can safely use makeObjectsPerformSelector:because the subviewsproperty will return a copyof the array of subviews:

对于UIView(仅限 iOS 开发),您可以安全地使用,makeObjectsPerformSelector:因为该subviews属性将返回子视图数组的副本

[[someUIView subviews]
 makeObjectsPerformSelector:@selector(removeFromSuperview)];

Thank you to Tommyfor pointing out that makeObjectsPerformSelector:appears to modify the subviewsarray while it is being enumerated (which it does for NSView, but not for UIView).

感谢Tommy指出它makeObjectsPerformSelector:似乎subviews在枚举数组时修改了它(它对NSView,但对 无效UIView)。

Please see this SO questionfor more details.

有关更多详细信息,请参阅此 SO 问题

Note:Using either of these two methods will remove every view that your main view contains and release them, if they are not retained elsewhere. From Apple's documentation on removeFromSuperview:

注意:使用这两种方法中的任何一种都将删除主视图包含的每个视图并释放它们,如果它们没有保留在其他地方。从 Apple 的关于removeFromSuperview的文档中:

If the receiver's superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.

如果接收者的superview 不为nil,则该方法释放接收者。如果您打算重用该视图,请确保在调用此方法之前保留它,并确保在完成它或将其添加到另一个视图层次结构后适当地释放它。

回答by Matthew McGoogan

Get all the subviews from your root controller and send each a removeFromSuperview:

从根控制器获取所有子视图并发送每个子视图 removeFromSuperview:

NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
    [v removeFromSuperview];
}

回答by Jeehut

In Swiftyou can use a functional approachlike this:

Swift 中,您可以使用这样的函数式方法

view.subviews.forEach { 
for subview in view.subviews {
    subview.removeFromSuperview()
}
.removeFromSuperview() }

As a comparison, the imperative approachwould look like this:

作为比较,命令式方法如下所示:

[[yourView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

These code snippets only work in iOS / tvOSthough, things are a little different on macOS.

这些代码片段仅适用于iOS / tvOS,但在 macOS 上有些不同。

回答by Mohd Rahib

If you want to remove all the subviews on your UIView (here yourView), then write this code at your button click:

如果要删除 UIView 上的所有子视图(此处yourView),请在单击按钮时编写此代码:

for (int i=mySuperView.subviews.count-1; i>=0; i--)
        [[mySuperView.subviews objectAtIndex:i] removeFromSuperview];

回答by Daniel

This does only apply to OSX since in iOS a copy of the array is kept

这仅适用于 OSX,因为在 iOS 中保留了数组的副本

When removing all the subviews, it is a good idea to start deleting at the end of the array and keep deleting until you reach the beginning. This can be accomplished with this two lines of code:

删除所有子视图时,最好从数组末尾开始删除并继续删除直到到达开头。这可以通过以下两行代码完成:

for var i=mySuperView.subviews.count-1; i>=0; i-- {
    mySuperView.subviews[i].removeFromSuperview();
}

SWIFT 1.2

斯威夫特 1.2

for subview in mySuperView.subviews.reverse() {
    subview.removeFromSuperview()
}

or (less efficient, but more readable)

或(效率较低,但更具可读性)

[[someUIView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

NOTE

笔记

You should NOTremove the subviews in normal order, since it may cause a crash if a UIView instance is deleted before the removeFromSuperviewmessage has been sent to all objects of the array. (Obviously, deleting the last element would not cause a crash)

您应该删除子视图是正常的顺序,因为这可能会导致崩溃,如果前一个UIView实例被删除removeFromSuperview的消息已发送到阵列的所有对象。(显然,删除最后一个元素不会导致崩溃)

Therefore, the code

因此,代码

view.subviews.forEach { 
for (UIView *view in [self.view subviews]) 
{
 [view removeFromSuperview];
}
.removeFromSuperview() }

should NOTbe used.

应该被使用。

Quote from Apple documentation about makeObjectsPerformSelector:

引用Apple 文档中关于 makeObjectsPerformSelector 的内容

Sends to each object in the array the message identified by a given selector, starting with the first object and continuing through the array to the last object.

将给定选择器标识的消息发送到数组中的每个对象,从第一个对象开始并继续通过数组到最后一个对象。

(which would be the wrong direction for this purpose)

(为此目的,这将是错误的方向)

回答by William Hu

Try this way swift 2.0

试试这种方式 swift 2.0

view.subviews.forEach { 
extension UIView {
    func removeAllSubviews() {
        for subview in subviews {
            subview.removeFromSuperview()
        }
    }
}
.removeFromSuperview() }

回答by Shahzaib Maqbool

Use the Following code to remove all subviews.

使用以下代码删除所有子视图。

- (void)removeAllSubviews
{
    for (UIView *subview in self.subviews)
        [subview removeFromSuperview];
}

回答by MAGiGO

##代码##

回答by mixel

Using Swift UIViewextension:

使用 SwiftUIView扩展:

##代码##

回答by Rickster

In objective-C, go ahead and create a category method off of the UIView class.

在 Objective-C 中,继续从 UIView 类创建一个类别方法。

##代码##