views:

222

answers:

1

Im trying to get the selected index of a NSComboBox that has a datasource by using indexOfSelectedItem.

[combobox setUsesDataSource:YES];
[combobox setDataSource:dataSource];
[combobox selectItemAtIndex:1];
int idx =[combobox indexOfSelectedItem];

idx will always returns -1;

Getting the same results even when defining a internal list for the NSComboBox in InterfaceBuilder.

Is there any other way to retrieve the selected index?

A: 

You might try calling [combobox numberOfItems]; just to make sure that you have items in the list.

Additionally, calling selectItemAtIndex:1 should fire an NSComboBoxSelectionDidChangeNotification that you can listen for to make sure it changes.

Edit 1: Also, did you make sure to select "Uses Data Source" on the "Attributes Inspector" (Command-1 shortcut key) for the ComboBox in Interface Builder? By default, it uses the internal list, which would conflict with the DataSource method that you are attempting to use.

Edit 2: Never mind about that previous comment, your code calls it explicitely.

Topher Fangio
Got it working now, i don't know what was wrong, might have done something wrong in IB.
Martinj