如何将可可绑定编写为代码而不是在 Interface Builder 中?
在我的模型中,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 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]
.