I don't know about you, but I keep forgetting how to do this, hence this post as a reminder so that I don't have to search for it for the next project ;-)
The basic idea is to add the necessary functions to the FirstResponder and connect the menu items to the first responder.
The first responder can of course be found in your xib file, the MainMenu.xib.
In Xcode 6.3 the sequence of actions is the following:
- Open MainMenu.xib in Xcode.
- Select the First Responder.
- Select the "Attributes Inspector"
- An empty field "User Defined" is now visible. Click the "+" button to add new actions. These actions will be the name of your new functions. Suppose you want to add a menu item "Transpose Words" to the Edit Menu, and you have defined the function "transposeWords" in your NSView child. (This can be any class that can become the first responder, but we will use a child class of NSView here). The function must have the signature "name(sender: AnyObject?), after clicking the "+" sign, select the new name (called "action:") and rename it to "transposeWords:" (beware the colon sign!). Leave its parameter as "id".
- Add the menu & menu item as necessary. In the example add in the "Edit" menu an item named "Transpose Words". Open the Menu, open the Edit Item menu, open the Menu and now add a Menu Item through either drag & drop from the Utilities or by simply copy & paste of an existing item in the menu.
- Rename the new menu item "Transpose Words" and give it a Key Equivalent if necessary.
- Select the "Outlets" inspector.
- Remove any existing connections (possible if you used C&P)
- Drag a connection from the little circle behind "Sent actions->Selector" to the First Responder (in the navigator), a popup appears.
- Select from the pop-up the function you need to have triggered by the menu item. You should now see a new connection in the Outlet Inspector.
2015.05.19: Don't be surprised when you close and reopen the project that in step 4 above the field is empty again. The functions you defined previously are still available.
Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH
We don't get the world we wish for... we get the world we pay for.