带有绑定的 NSComboBox 和 NSTextField
我有一个 NSArrayController,我将其填充到 awakeFromNib 方法中。数据具有键:id
、name
和 description
。我有一个 ComboBox 和一个 TextField 绑定到 NSArrayController,第一个带有名称,第二个带有 id。如果我更改组合框中的选择,我希望文本字段中的值发生更改,反之亦然。我阅读了 TextField 和 ComboBox 绑定的文档,但我不明白如何实现这一点。
I have an NSArrayController that I fill in the awakeFromNib
method. The data has the keys: id
, name
, and description
. I have a ComboBox and a TextField bound to the NSArrayController the first with the name and the second with the id. If I change the selection in the ComboBox I want the value in the TextField to change, and vice-versa. I read the docs for TextField and ComboBox bindings, but I didn't understand how to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的技巧是您需要在其他地方放置 NSComboBox 的值。 NSArrayController 可以很好地向 NSComboBox 提供库存值,但是您可以在 NSComboBox 中键入可能不在 NSArrayController 的 contentArray 中的任意值,因此您需要在其他地方放置该值也就不足为奇了。我可以通过在 AppDelegate 上放置一个简单的值来快速模拟它,如下所示:
然后在实现中:
然后对于绑定,我将其设置如下:
NSComboBox:
连续更新值
)NSTextField:
连续更新值
)如果您希望将输入的新值添加到我很遗憾地说,仅使用这两个控件和绑定并不容易实现该数组。这有点复杂。但简单情况的技巧是,除了用于向 NSComboBox 提供预加载值的 NSArrayController 之外,您还需要其他位置来存储值。
The trick here is that you need somewhere else to put the value of the NSComboBox. The NSArrayController is fine for providing the stock values to the NSComboBox, but you can type arbitrary values into an NSComboBox that might not be in the NSArrayController's contentArray, so it's not surprising that you need somewhere else to put the value. I was able to mock this up quickly by just putting a simple value on the AppDelegate like this:
Then in the implementation:
Then for the bindings, I set it up like this:
NSComboBox:
Continuously Updates Value
if you want the NSTextField to be updated letter-by-letter as you're typing in the NSComboBox)NSTextField:
Continuously Updates Value
if you want the NSComboBox to be updated letter-by-letter as you're typing in the NSTextField)If you want new values you type in to be added to the array, I'm sorry to say, that's not readily doable with just these two controls and bindings. That's a fair bit more complicated. But the trick for the simple case is that you need some place to store the value other than the NSArrayController you're using to provide pre-loaded values to the NSComboBox.