对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) -> Intto 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.
更多推荐
发布评论