NSComboBox在下拉菜单中不显示当前选定的值(NSComboBox not showing current selected value in dropdown)

编程入门 行业动态 更新时间:2024-10-20 21:11:39
NSComboBox在下拉菜单中不显示当前选定的值(NSComboBox not showing current selected value in dropdown)

对Mac应用程序编程很新,我遇到了问题。 在我的NSComboBoxes上,当重新打开下拉菜单时,它们不会突出显示当前选定的项目。 我可能只是失明,或者默认的高亮颜色是透明的。 我只是不知道。 看到下面的图片,我得到了什么。

在这一点上,我写了很少的代码,并且我的故事板上的一切都正在完成。 只是有点困惑为什么它没有做一些应该非常简单的事情。 猜猜我花了太多时间在iOS的土地上。

因此,决定重新创建整个项目,只是一个NSCombobox示例,以证明在运行Xcode 9.2时发生这种事情对我来说很简单。

我连接的唯一插座是NSComboBox的委托和数据源。 并且在NSComboBox上更改的唯一设置是启用数据源,并更改行为toe Selectable。 运行此组合的代码非常简单:

import Cocoa class ViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource { @IBOutlet weak var combo: NSComboBox! var dataSource: [String] = ["Off"] override func viewDidLoad() { super.viewDidLoad() for index in 1...512 { dataSource.append("\(index)") } combo.reloadData() combo.selectItem(at: 0) } func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? { return dataSource[index] } func numberOfItems(in comboBox: NSComboBox) -> Int { return dataSource.count } }

根据Apple文档,下拉列表应显示所选项目: Apple NSComboBox Docs

So new to programming Mac apps and am running into a problem. On my NSComboBoxes they do not highlight the currently selected item when reopening the dropdown. I might just be blind or maybe the default highlight color is transparent. I just don't know. See the below image for what I am getting.

At this point I have very little code written and everything is being done on my Storyboard. Just a little confused why it isn't doing something that should be quite simple. Guess I spent too much time in iOS land.

So decided to recreate the entire project as just one single sample NSCombobox to demonstrate that this happens on something simple for me while running Xcode 9.2.

The only outlet I have hooked up are the delegates and datasource for the NSComboBox. And the only settings changed on the NSComboBox are to enable datasource, and to change the behavior toe Selectable. The code to run this combo is just as simple:

import Cocoa class ViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource { @IBOutlet weak var combo: NSComboBox! var dataSource: [String] = ["Off"] override func viewDidLoad() { super.viewDidLoad() for index in 1...512 { dataSource.append("\(index)") } combo.reloadData() combo.selectItem(at: 0) } func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? { return dataSource[index] } func numberOfItems(in comboBox: NSComboBox) -> Int { return dataSource.count } }

According to Apple documentation here the dropdown should show the selected item: Apple NSComboBox Docs

最满意答案

该列表将在您离开它的地方打开。 实现NSComboBoxDataSource方法

optional func comboBox(_ comboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int

让组合框知道要选择哪一行。

返回值

与指定字符串匹配的项的索引,如果没有项匹配则为NSNotFound。

讨论

NSComboBox对象使用此方法将弹出列表的选定项与文本字段的内容同步。 如果您未实现此方法,则接收器不会将弹出列表的所选项目与文本字段的内容同步。

The list opens where you left it. Implement the NSComboBoxDataSource method

optional func comboBox(_ comboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int

to let the combobox know which row to select.

Return Value

The index for the item that matches the specified string, or NSNotFound if no item matches.

Discussion

An NSComboBox object uses this method to synchronize the pop-up list’s selected item with the text field’s contents. If you don’t implement this method the receiver does not synchronize the pop-up list’s selected item with the text field’s contents.

更多推荐

本文发布于:2023-08-01 03:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   菜单   NSComboBox   showing   dropdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!