无法从 NSComboBox 检索选定的索引

发布于 2024-08-09 15:30:52 字数 337 浏览 2 评论 0原文

我尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-08-16 15:30:52

您可以尝试调用 [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文