0

I am working on code that dynamically builds a window from scratch in macOS. I would like to add NSComboBox to the list of supported controls. I am using a generic window delegate that contains a window controller that handles all the events for all the controls. As each control is added, the various action selectors or notification handlers are added for that control.

(This is code that I inherited.)

So the issue is this. I have the combo box basically working. When I type into it, I get the expected NSControlTextDidChangeNotification notifications. And when I select from the popup I get the expected NSComboBoxSelectionDidChangeNotification notifications. The issue I am having is that when the text changes because of the NSComboBoxSelectionDidChangeNotification notification, the NSControlTextDidChangeNotification does not fire. But the NSComboBoxSelectionDidChangeNotification happens before the edit text box changes.

I would like to forward a notification to the client code each time the text changes, irrespective of how it changes. The problem I have is that I can't seem to get notified when the text changes because of a selection from the popup.

Here is a simplfied version of my setup (where view is the control just added):

    if ([view isKindOfClass:[NSComboBox class]])
    {
        NSComboBox *comboboxcontrol = (NSComboBox *) view;
        SEL mySelector = @selector(comboBoxPopupChange:);
        NSInteger tag = [comboboxcontrol tag];
        if (tag != 0)
        {
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc addObserver:self selector:mySelector name:NSComboBoxSelectionDidChangeNotification object:comboboxcontrol];
        }
    }
    if ([view isKindOfClass:[NSTextField class]])
    {
        NSTextField *textfield = (NSTextField *) view;
        NSInteger tag = [textfield tag];
        if (tag != 0)
        {
            SEL textchangeSelector = @selector(editTextChange:);
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc addObserver:self selector:textchangeSelector name:NSControlTextDidChangeNotification object:textfield];
        }
    }

I have verified that both blocks of code execute and both selectors fire. The problem in a nutshell is that NSComboBoxSelectionDidChangeNotification fires before the control text changes but there is no subsequent NSControlTextDidChangeNotification to tell me that it changed. (The text-changed notifications only fire when I type in the edit box.)

rpatters1
  • 382
  • 1
  • 11
  • Can't you use the action of the `NSComboBox`? – Willeke Jul 28 '22 at 22:13
  • The action doesn't seem to fire, at least when selecting from the popup. The code I inherited was doing that (and not getting any hits), so I changed to the above. I found another answer on this site that was having the same issue. – rpatters1 Jul 29 '22 at 03:13
  • Did you set the target? – Willeke Jul 29 '22 at 07:21
  • Here is the code I replaced with the NSComboBoxSelectionDidChangeNotification observer: ```objective-c NSComboBox *comboboxcontrol = (NSComboBox *) view; SEL mySelector = @selector(comboBoxPopupChange:); [comboboxcontrol setAction:mySelector]; ``` – rpatters1 Jul 30 '22 at 11:55
  • Have you tried setting the target? `comboboxcontrol.target = nc` – Willeke Jul 30 '22 at 13:04
  • Oh, I see. That was a good suggestion, but it was already being done in the code. – rpatters1 Aug 01 '22 at 00:11
  • Post a [mre] please. – Willeke Aug 02 '22 at 06:11
  • Thank you for your continued interest. The code above *is* working, just firing before instead of after the edit text changes. I posted the question thinking there might be something obvious I missed, but apparently there wasn't. As it turns out the Windows cbobox behaves quite similarly (firing the selection change before the text changes), so I'm probably going to leave it as-is. – rpatters1 Aug 03 '22 at 11:19

0 Answers0