无法从 NSComboBox 检索选定的索引
我尝试使用indexOfSelectedItem 获取具有数据源的NSComboBox 的选定索引。
[combobox setUsesDataSource:YES];
[combobox setDataSource:dataSource];
[combobox selectItemAtIndex:1];
int idx =[combobox indexOfSelectedItem];
idx 将始终返回 -1;
即使在 InterfaceBuilder 中为 NSComboBox 定义内部列表,也能获得相同的结果。
还有其他方法可以检索选定的索引吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试调用
[combobox numberOfItems];
只是为了确保列表中有项目。此外,调用
selectItemAtIndex:1
应触发 NSComboBoxSelectionDidChangeNotification 您可以监听它以确保它发生变化。编辑1:另外,您是否确保在Interface Builder中的ComboBox的“属性检查器”(Command-1快捷键)上选择“使用数据源”?默认情况下,它使用内部列表,这会与您尝试使用的 DataSource 方法冲突。
编辑 2: 不要介意之前的注释,您的代码会明确调用它。
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.