In my Cocoa app I have two NIB/XIB files that I need to connect:
MainMenu.xib: contains a custom object for the NSApplication delegate object and connects it to the proper outlet in the NSApplication
placeholder.
ContextMenu.xib: sets up an NSMenu
; one entry is supposed to open the preferences dialog
My custom app delegate defines an IBAction
to bring up the Preferences window for my app.
How can I connect the NSMenuItem (second NIB) for showing the preferences to the action defined in the application delegate (first NIB)?
The Docs say this is supposed to be easy, but they fail to mention how exactly to do this in Interface Builder:
If the menu item refers to an application-level command, you can implement that command directly in the application delegate or just have the delegate forward the message to the appropriate object elsewhere in your application.
I somehow need to access the app delegate in the second NIB, tell Interface Builder that it is of my custom class (so it knows about the custom IBAction), and connect it to the action of the menu item.
Thanks for any pointers!
If the other objects are in the responder chain, then you can just hook the action up to the first responder.
Notice the "if", though.
As Maurice Kelly mentions, your App Delegate is already part of the responder chain, so you can use that: Define a custom action on the First Responder (in Interface Builder) and a corresponding action on your App Delegate. If you have many actions that could clutter up the App Delegate, though, so you might want to use this architecture only for simple apps.