gpt4 book ai didi

ios - 从一个 View 中删除一个 View 并将其添加到另一个 View

转载 作者:可可西里 更新时间:2023-11-01 04:44:35 26 4
gpt4 key购买 nike

我正在尝试找到 Nib 中的所有 View 并将它们添加到我的内容 View 中。

这就是我所拥有的,它成功地从 self.view 中删除了 View ,但没有将它添加到 self.contentView

for (UIView *view in self.view.subviews) {
if (view.tag != 666) {
[view removeFromSuperview];
[self.contentView addSubview:view];
}
}

如有任何帮助,我们将不胜感激。

最佳答案

您的代码中的问题是,当您调用 removeFromSuperview 时,父 View 将释放该 View 。无需调用 removeFromSuperview,只需将其添加为另一个 View 的 subview 即可将其从当前父 View 中移除。

所以使用:

for (UIView *view in self.view.subviews)
{
if (view.tag != 666)
{
[self.contentView addSubview:view];
}
}

根据 UIView Class Reference :

addSubview:

Adds a view to the end of the receiver’s list of subviews.

- (void)addSubview:(UIView *)view

Parameters

view

The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews. 

Discussion

This method retains view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

关于ios - 从一个 View 中删除一个 View 并将其添加到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com