Im tryi开发者_StackOverflowng 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?
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.
精彩评论