NSComboBox - 获取选定信息和NSComboBoxDataSource

发布于 2024-08-10 06:57:46 字数 978 浏览 3 评论 0原文

在我的一生中,我一直被 NSComboBox 所困扰。

我创建了一个符合 NSComboBoxDataSource 协议的对象,并实现了:

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

我将 NSComboBox 的实例设置为使用数据源,并将该对象设置为源。效果很好,我的实现返回项目的数量,并返回特定索引处项目的 NSString 值。

然后我决定当选择某件事时我想做某事,这就是我的问题开始的地方。 NSComboBoxDataSource 协议中没有明显的方法可以重写来处理组合框中项目的选择。

因此,我也让我的对象符合 NSComboBoxDelegate 并实现:

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

不幸的是,与选择时的 NSTableView 不同,通知的对象是 NSComboBox 而不是对象所选项目的。 “很好”,我告诉自己,我将调用 NSComboBox 方法:

- (id)objectValueOfSelectedItem;

这应该返回所选的项目,我可以从那里开始。但是,只有当 usesDataSource 设置为 NO 时才调用该方法,这不是我的情况。当我使用它时,警告开始出现。

所以,我的问题是,当您使用数据源时,处理 NSComboBox 选择的正确方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

宣告ˉ结束 2024-08-17 06:57:46

我想你想要 indexOfSelectedItem 而不是 objectValueOfSelectedItem。然后,由于您是数据源,您应该能够调用自己的 comboBox:objectValueForItemAtIndex: 方法。

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.

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