如何将可可绑定编写为代码而不是在 Interface Builder 中?

发布于 2024-09-03 14:00:20 字数 451 浏览 1 评论 0原文

在我的模型中,我有一个 NSMutableArray 来跟踪不断变化的元素数量。 在我看来,我得到了一个显示元素数量的 NSTextField
视图从 nib 文件中取消归档并分配/初始化模型。因此,它知道模型和所包含的数组。

我按如下方式建立了连接。在“Interface Builder”的文本字段中,我添加了一个“Cocoa Binding”“路径”,如下所示:myModell.myArray.@count。通过这个我可以访问 count 属性(这是必须的,因为数组本身改变)。
绑定基于键值合规性,我在模型中建立了键值合规性,以便可以访问数组。但键值合规性并不是问题的一部分。

我的问题:如何将绑定放入源代码中而不是将其写入Interface Builder

In my model I got an NSMutableArray that keeps track of a changing number of elements.
In my view I got a NSTextField that shows the number of elements.
The view gets unarchived from the nib file and alloc/inits the modell. Therefore, it knows about the modell and the contained array.

I established the connection as follows. In the Interface Builder at the textfield I added a Cocoa Binding "path" like this: myModell.myArray.@count. By this I can access the count property (which is a must since the array itself does not change).
The binding is based on key-value compliance, which I established at the model so the array can be accessed. But key-value compliance is not part of the questions.

My question: How can I put the binding into the source code and not writing it into Interface Builder?

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

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

发布评论

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

评论(1

桃扇骨 2024-09-10 14:00:20

使用 NSKeyValueBindingCreation 协议。您发送类似 [someObject bind:@"value" toObject:myModel withKeyPath:@"myArray.@count" options:nil] 的内容。

With the NSKeyValueBindingCreation protocol. You send something like [someObject bind:@"value" toObject:myModel withKeyPath:@"myArray.@count" options:nil].

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