xcodecocoaswift3nscombobox

Preselecting a NSComboBox Entry


I have a modal window that contains a NSComboBox. "Uses Data Source" is set to turn and the combobox correctly lists the entries. The view controller is the delegate for the data source. When I call the modal window to update an existing object I would like the combobox to display the selected entry if there is one. How do I do this?

I've tried to access the entries in viewWillAppear. I get and error saying there are no entries. The various print statements I have in the code indicate that the values aren't loaded until the drop down arrow is clicked. The two function I'm supplying as the delegate are:

func numberOfItems(in comboBox: NSComboBox) -> Int
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any?

Would it be possible to set the selected entry in the second function above?


Solution

  • As NSComboBox is a subclass of NSTextField you should be able to set its text by setting the stringValue property of your comboBox.

    self.comboBox.stringValue = "Hello World"