NSComboBox

编程入门 行业动态 更新时间:2024-10-28 08:22:47
NSComboBox - 获取选定的信息和NSComboBoxDataSource(NSComboBox - Obtaining selected information and NSComboBoxDataSource)

对于我来说,我一直被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时usesDataSource调用该方法,但这不是我的情况。 当我使用这个时,警告开始飞行。

所以,我的问题是,当您使用数据源时,处理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?

最满意答案

我认为你想要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.

更多推荐

本文发布于:2023-08-01 11:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSComboBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!