Cocoa Programming, Delegate Installation

I am transitioning from iOS to Cocoa and trying to get confused in my first few programs. I thought it would be easy to add an NSComboBox to my form, well, that was. I added <NSComboBoxDelegate, NSComboBoxDataSource> to my interface, two 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 the drag and drop of combobox into the application delegate (which is the only class in my regular application by default) and connected the delegate and data source, but none of these events fire. I thought the delegate application was correct, but since it didn’t work, I also tried “file owner” and “application”. I did not think that they would work, but they did not.

What is the correct way to connect the delegate / data source for an NSComboBox in a Cocoa application?

Thanks!

+7
source share
2 answers

If you really implemented these methods in your spcAppDelegate.m file, you can double check that the Uses Data Source checked for NSComboBox in the nib file in Interface Builder:

enter image description here

Note that it was not installed by default in the quick scan project that I created. If you start without this check box, set the following command for the console when starting the application:

 NSComboBox[2236:403] *** -[NSComboBox setDataSource:] should not be called when usesDataSource is set to NO NSComboBox[2236:403] *** -[NSComboBoxCell setDataSource:] should not be called when usesDataSource is set to NO 

While the NSComboBox class reference is somewhat useful, when I first found out, I found that if there were helper manuals related to the class, they were much more useful in understanding how the class should be used in practice. If you look at the top of the link for the NSComboBox class in the Companion Guide , you will see Combo Box Programming Topics .

To set up a combo box that uses a data source, you can use something like the following:

spcAppDelegate.h:

 #import <Cocoa/Cocoa.h> @interface spcAppDelegate : NSObject <NSApplicationDelegate, NSComboBoxDelegate, NSComboBoxDataSource> { IBOutlet NSWindow *window; IBOutlet NSComboBox *comboBox; NSMutableArray *comboBoxItems; } @property (assign) IBOutlet NSWindow *window; @end 

spcAppDelegate.m:

 #import "spcAppDelegate.h" @implementation spcAppDelegate @synthesize window; - (id)init { if ((self = [super init])) { comboBoxItems = [[NSMutableArray alloc] initWithArray: [@"Cocoa Programming setting the delegate" componentsSeparatedByString:@" "]]; } return self; } - (void)dealloc { [comboBoxItems release]; [super dealloc]; } - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox { return [comboBoxItems count]; } - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index { if (aComboBox == comboBox) { return [comboBoxItems objectAtIndex:index]; } return nil; } - (void)comboBoxSelectionDidChange:(NSNotification *)notification { NSLog(@"[%@ %@] value == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [comboBoxItems objectAtIndex: [(NSComboBox *)[notification object] indexOfSelectedItem]]); } @end 

Sample project: http://github.com/NSGod/NSComboBox .

+15
source

I had a similar situation yesterday, until I remembered that I connected the File Owner data source to IBOutlet in IB:

enter image description here

0
source

All Articles