How can I get a populated combobox to display its options in Swift?

一笑奈何 提交于 2019-12-05 02:38:29

问题


With this code I'm able to populate the items in an NSComboBox, but the combobox itself is always empty when you expand it in the app.

The number of items being logged is accurate. I know this is rather basic, but I've read and re-read the docs at developer.apple.com and have looked for examples of comboboxes in Swift to no avail, which is quite surprising to me.

@IBOutlet var someComboBox: NSComboBox!

override func viewDidLoad() {
    super.viewDidLoad()

    someComboBox = NSComboBox()
    someComboBox.addItemWithObjectValue("Foo")
    someComboBox.addItemWithObjectValue("Bar")
    someComboBox.addItemWithObjectValue("Baz")
    println(someComboBox.numberOfItems)
}

and the result:


回答1:


Your comboBox is an IBOutlet, so the object is created for you already by the Storyboard; you just need to initialize it. So remove:

someComboBox = NSComboBox()

because that is allocating a new combo box and overwriting the reference to the one created in the Storyboard. Your new comboBox is not the one that is on screen.



来源:https://stackoverflow.com/questions/28114909/how-can-i-get-a-populated-combobox-to-display-its-options-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!