How to get NSComboBox in a NSToolbar to send messages to the datasource and delegate

786 views Asked by At

I have an NSComboBox in a NSToolbar and I want the combo box to send messages to its datasource and delegate.

My class adopts the NSComboBoxDelegate and NSComboBoxDataSource protocols. I create my combo box like this

    NSRect comboBoxRect = NSMakeRect(0, 0, 175, 20);
    NSComboBox *sourceComboBox = [[NSComboBox alloc] initWithFrame:comboBoxRect];
    [sourceComboBox setDataSource:self];
    [sourceComboBox setDelegate:self];
    [sourceComboBox setUsesDataSource:YES];
    [sourceComboBox setEditable:NO];

    NSToolbarItem *sourceComboBoxItem = [[NSToobarItem alloc] initWithItemIdentifier:@"MyID"];
    [sourceComboBoxItem setView:sourceComboBox];
    [sourceComboBoxItem setMinSize:NSMakeSize(175*1.2, SEGMENT_HEIGHT)];
    [sourceComboBoxItem setMaxSize:NSMakeSize(175*1.2, SEGMENT_HEIGHT)];
    [sourceComboBoxItem setDelegate:self];
    [sourceComboBoxItem setTag:4];

I also implement the following datasource and delegate methods had have break points in them, but nothing is being sent to those methods.

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
- (void)comboBoxSelectionDidChange:(NSNotification *)notification;

Any suggestions on what I may be doing wrong? Thank you.

1

There are 1 answers

0
David On BEST ANSWER

I got it to work by creating the combo box in Interface Builder and then setting my custom class as the datasource to the combo box cell and the delegate to the combo box. Instead of adopting NSComboBoxDataSource, I adopted NSComboBoxCellDataSource instead.

I don't know why my first method didn't work but this new method seems to do the just fine.