2
votes

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.

Dropdown Image

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.

Sample Project Settings

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

1
Are you sure there is no trailing space in your values or in your combo box stringValue? It will only be selected if both values are exactly the same.Leo Dabus
Shouldn't be I disabled being able to type in the box, and so you can only select items. It doesn't even select my initial default value when I use combo.selectItem(at: 0). It shows a highlight when I select an item, and correctly returns the selected index when that is called but does not show the selected item when reopening the dropdown.Binary Platypus
myComboBox.stringValue = "490"El Tomato
Nope using stringValue instead of selectItem(at:) doesn't change anything.Binary Platypus
How do you fill the list? Is this the only "490" in the list? Did you add a number formatter?Willeke

1 Answers

6
votes

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.