NSComboBox - retrieving selected information and NSComboBoxDataSource

In my life, NSComboBox constantly NSComboBox .

I created an object that conforms to the NSComboBoxDataSource protocol and is implemented:

 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox; - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index; 

I installed an instance of my NSComboBox to use the data source and set this object as the source. This works fine, my implementation returns the number of elements and returns the NSString value for the element at specific indices.

Then I decide that I want to do something when something is selected, this is where my problems begin. There is no obvious override method in the NSComboBoxDataSource protocol for handling selection of items in a combo box.

So, I also have my object corresponding to NSComboBoxDelegate and implementing:

 - (void)comboBoxSelectionDidChange:(NSNotification *)notification; 

Unfortunately, unlike NSTableView when selected, the notification object is NSComboBox not the object of the selected item. “Good,” I tell myself, I will call the NSComboBox method:

 - (id)objectValueOfSelectedItem; 

This should return the item that is selected, and I can go from there. However, this method should be called ONLY when usesDataSource set to NO , which is not my business. Warnings start flying when I use this.

So my question is how to handle the selection of NSComboBox when using a data source?

+4
source share
1 answer

I think you want indexOfSelectedItem instead of objectValueOfSelectedItem. Then, since you are a data source, you can call your own comboBox:objectValueForItemAtIndex: method.

+15
source

All Articles