多个 XIB 具有相同的文件所有者

发布于 2024-09-17 21:14:37 字数 574 浏览 3 评论 0原文

我正在尝试创建一个两个视图、单个控制器应用程序,如下所示:我有两个 XIB。每个文件都有相同的文件所有者。

作为测试,我在每个 XIB 上放置了一个 UILabel。我已将文件所有者连接到每个 XIB 中的 UILabel。出口属性是相同的。

当我使用 loadNibNamed 实例化笔尖时,我还将“所有者”设置为文件所有者的实例,例如:

nib=[[NSBundle mainBundle] loadNibNamed:@"ONE" owner:OWNER options:nil];
nib=[[NSBundle mainBundle] loadNibNamed:@"TWO" owner:OWNER options:nil];

现在,在 OWNER 中,如果我调用,

[myLabel setText:@"Hello World"];

我只会在笔尖 2 中看到标签更新。

如果我创建每个 NIB 独有的附加 UILabel,那么我可以正确更新和查看它们。看来我只能从文件所有者的属性到每个 NIB 建立一个连接。

有什么想法吗?

I am trying to create a two view, single controller application as follows: I have two XIB's. Each with the same File's Owner.

As a test, I have placed a UILabel on each XIB. I have connected the File Owner to the UILabel in each XIB. The outlet property is the same.

When I instantiate the nib using loadNibNamed I also set the 'owner' to the instance of File's Owner, e.g.:

nib=[[NSBundle mainBundle] loadNibNamed:@"ONE" owner:OWNER options:nil];
nib=[[NSBundle mainBundle] loadNibNamed:@"TWO" owner:OWNER options:nil];

Now, in OWNER, if I call

[myLabel setText:@"Hello World"];

I see the label update only in nib TWO.

If I create additional UILabels that are unique to each NIB then I can properly update and view them. It seems that I can only have one connection from the property on File's Owner to each NIB.

Any ideas?

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

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

发布评论

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

评论(3

撩人痒 2024-09-24 21:14:37

您想要的是 IBOutletCollection。这允许您将一个属性分配给多个 nib 元素,并同时讨论整个组。

What you want is an IBOutletCollection. That allows you to assign a property to more than one nib element, and talk about the entire group all at once.

寂寞陪衬 2024-09-24 21:14:37

一个 IBOutlet 只能指向一个对象。您要使用的每个 IBOutlet 都需要两个。

an IBOutlet can only point to one object. You will need two of every IBOutlet you want to use.

雪花飘飘的天空 2024-09-24 21:14:37

这是很旧的并且没有太多人浏览,但我忍不住注意到为什么这不起作用。您正在传递所有者的同一个实例。创建文件所有者的两个实例,您可以有两个不同的标签值。事实上,您没有理由不能将整个项目中的所有插座分配给一个对象类,尽管您可能不想这样做。另一件需要考虑的事情是,您是否不应该在这里使用继承,通过创建一个超类,将所有公共出口连接到该类,然后连接到具有唯一出口的子类。事实上,由于您必须通过笔尖标识符或与它们关联的类来调用它们来区分您的笔尖,我认为最好的做法是关联单独的类并使用继承来覆盖它们之间的重叠。

This is very old and not much viewed, but I can't help but notice why this doesn't work. You're passing in the same instance of the owner. Make two instances of the file's owner and you can have two different label values. There's no reason you couldn't assign all of your outlets in your whole project to one Object class in fact, although you probably wouldn't want to do this. Another thing to think about is whether you shouldn't be using inheritance here, by making a superclass, connecting all of the common outlets to that class and then a subclass with unique outlets. Indeed, since you will either have to distinguish your nibs by calling them by nib identifier or by a the class associated with them I think it's better practice to associate separate classes and use inheritance to cover the overlap between them.

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