为 NSPopupButton 设置默认选择?

发布于 2024-09-04 01:39:20 字数 356 浏览 10 评论 0原文

是否可以在 NSPopupButton 上设置默认选择?我有一个允许用户选择他们想要设置的服务器类型,但由于 NSPopupButton 总是显示第一项,如果这是他们想要的类型,他们可能会忽略它。但是,即使正在显示该项目,调用 -selectedItem 也会返回 (null)。如果用户首先从菜单中选择一个项目,则一切正常。

Button 的 content 和 contentValues 绑定到同一个数组控制器,而数组控制器又绑定到 NSDictionary 的 keys 属性。我尝试将 selectedIndex 绑定到控制器中的变量并在代码中更新它,但它没有效果。 (我可能只是绑定错误......)我如何默认选择第一项?

预先感谢!
球体猫1

Is it possible to set a default selection on an NSPopupButton? I have one that allows the user to select the type of server they want to set up, but since an NSPopupButton always shows the first item, they may ignore it if that's the type they want. However, even though that item is being displayed, calling -selectedItem returns (null). Everything works fine if the user picks an item from the menu first.

The Button's content and contentValues are bound to the same Array Controller, which in turn is bound to the keys property of an NSDictionary. I've tried binding the selectedIndex to a variable in the controller and updating that in code, but it has no effect. (I may just be binding it wrong...) How can I select the first item by default?

Thanks in advance!
SphereCat1

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

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

发布评论

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

评论(1

守望孤独 2024-09-11 01:39:20

使用绑定时,您不需要也不应该直接从视图获取任何模型信息(无论是模型本身还是选择状态)。与拥有模型和所选索引的控制器交谈。

请注意,“index”对于 NSDictionary 没有任何意义,并且 keys 也不是 NSDictionary 的属性。 (事实上​​,如果您遇到异常,我不会感到惊讶,因为您的字典中没有键“keys”的对象。)它是一种方法,而不是访问器类型,所以当您 可以使用键值编码向字典询问该方法的值,但您不应该这样做。

您应该做的是使 模型对象 代表服务器类型,并保存这些类型的数组,并将数组控制器的内容绑定到值为该数组的属性。将弹出按钮的 contentValues 绑定到模型对象的 name 属性,该属性应保存每种服务器类型的本地化名称。

When using Bindings, you don't need to and shouldn't get any model info—neither the model itself nor selection state—from the views directly. Talk to the controller that owns the model and the selected indexes.

Note that “index” doesn't have any meaning for an NSDictionary, and keys is not a property of an NSDictionary. (Indeed, I would not be surprised if you were to get an exception because your dictionary does not have an object for the key “keys” in it.) It is a method, and not the accessor kind, so while you can ask the dictionary for the value of that method using Key-Value Coding, you should not.

What you should do is make model objects representing the server types, and hold an array of those, and bind the array controller's content to the property whose value is that array. Bind the pop-up button's contentValues to a name property of your model objects, which should hold the localized name of each server type.

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