Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSComboBox - Obtaining selected information and NSComboBoxDataSource

For the life of me, I am being continually stumped with NSComboBox.

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

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

I set the instance of my NSComboBox to use a Data Source, and set this object as the source. That works great, my implementation returns the number of items, and returns an NSString value for an item 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 method to override in the NSComboBoxDataSource protocol to handle the selection of items in the combo box.

So, I also have my object conform to NSComboBoxDelegate and implement:

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

Unfortunately, unlike NSTableView on selection, the notification's object is the NSComboBox not the object of the item selected. "Fine" 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, that method is to be called ONLY when usesDataSource is set to NO, which is not my case. Warnings start flying when I use this.

So, my question is, what is the proper way to handle NSComboBox selections when you are using a data source?

like image 858
MarkPowell Avatar asked Dec 07 '22 05:12

MarkPowell


1 Answers

I think you want indexOfSelectedItem instead of objectValueOfSelectedItem. Then since you're the data source you should be able to call your own comboBox:objectValueForItemAtIndex: method.

like image 119
nall Avatar answered Dec 21 '22 22:12

nall