为什么将 uilabel 添加到多个子视图不起作用?

发布于 2024-10-09 18:45:22 字数 210 浏览 0 评论 0原文

比如说,

UILabel *label = [[UILabel alloc] init]; ....

[view1 addSubview:label]; [view2 addSubview:标签];

addSubview 不保留对当前标签的引用吗?也就是说,它有自己的 UILabel 副本,但为什么该标签只显示在视图 2 上?

谢谢。

Say,

UILabel *label = [[UILabel alloc] init];
....

[view1 addSubview:label];
[view2 addSubview:label];

Doesn't addSubview retain a reference to the current label? That is, it has its own copy of the UILabel but why the label only shows up on view 2?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-10-16 18:45:22

这不起作用,因为视图只能有 1 个超级视图。因此,如果您想在不同视图中拥有同一标签的多个实例,您必须自己创建标签的副本并将其添加到另一个超级视图中。

引用自参考文献:

视图只能有一个超级视图。如果
view 已经有一个 superview 并且
view不是接收者,这个方法
删除之前的上一个超级视图
使接收者成为新的超级视图。

That does not work because view can have only 1 superview. So if you want to have multiple instances of same label in different views you have to create a copy of your label yourself and add it to another superview.

Quote from reference:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文