I’m moving on from iOS to Cocoa and trying to muddle through my first few programs. I thought it would be simple to add an NSComboBox to my form, well that part was. I added <NSComboBoxDelegate, NSComboBoxDataSource> to my interface, two data callbacks, and the notifier:
@interface spcAppDelegate : NSObject <NSApplicationDelegate,
NSComboBoxDelegate, NSComboBoxDataSource>
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
- (void)comboBoxSelectionDidChange:(NSNotification *)notification;
@end
I control dragged the combobox to the app delegate (which is the only class in my simple default app) and wired up the delegate and data source but none of those events fire. I thought app delegate was correct but since it didn’t fire, I also tried “file owner” and “application”. I didn’t think those would work and they didn’t.
Whats the right way to wire up the delegate/data source for an NSComboBox in a Cocoa app?
Thanks!
Provided you’ve actually implemented those methods in your
spcAppDelegate.mfile, you may want to double-check thatUses Data Sourceis checked for theNSComboBoxin the nib file in Interface Builder:Note that it wasn’t set by default in a quick test project I created. Running without that checkbox set should log the following to console when you launch the app:
While the NSComboBox Class Reference is somewhat helpful, when I was first learning, I found that if there were companion guides linked to for a class, those were much more helpful in understanding how one should use the class in practice. If you look at the top of the
NSComboBoxclass reference at the Companion Guide, you’ll see Combo Box Programming Topics.To set up a combo box that uses a data source, you could use something like the following:
spcAppDelegate.h:
spcAppDelegate.m:
Sample Project: http://github.com/NSGod/NSComboBox.