对于我来说,我一直被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.
更多推荐
发布评论