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.)