I have an NSPopUpButton that I have subclassed that I plan to use in multiple locations within my program. It’s subclassed because it has special functionality to it.
Normally to receive actions events, you create a method in the controller class and then connect it with interface builder to handle the action. For example if a user changed one of the options in the NSPopUpButton it would send a message to an interface builder action method such as below.
- (IBAction)sourcePopUpMenuChanged:(id)sender {
NSLog(@"sourcePopUpMenuChanged");
}
This is fine in most cases, but in my case I want to have it send the message to the subclass it’s self and not the controller class. I want to do this because I am setting a global variable behind the scenes to keep track of the index for the popUpMenu, since the popupmenu appears in many different windows in my application.
I’m aware that I could just create IBAction events for every window for this control and manually set the global variable everytime but this seems inefficient.
Is there a way that if ANY INSTANCES of my control receive an ‘Action’ Message, that I can run some kind of code, for example, like set a global variable for gIndex = PopUpMenu Index Selected.
Help is much appreciated!
To answer your question, you can override the method
sendAction:to:on yourNSPopUpButtonsubclass to do your stuff. Just don’t forget to callsuperso that the action message is forwarded to the regular receiver.When a control wants to send the configured action it uses the
sendAction:to:method which callssendAction:to:from:onNSApp(the singleNSApplicationinstance). This method follows the responder chain to find the final recipient of the action message.