Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.


Adding menu items and their actions

When developing OS-X applications its often necessary to create additional menu items and connect these to their handlers in our code.

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:
  1. Open MainMenu.xib in Xcode.
  2. Select the First Responder.
  3. Select the "Attributes Inspector"
  4. 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".
  5. 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.
  6. Rename the new menu item "Transpose Words" and give it a Key Equivalent if necessary.
  7. Select the "Outlets" inspector.
  8. Remove any existing connections (possible if you used C&P)
  9. Drag a connection from the little circle behind "Sent actions->Selector" to the First Responder (in the navigator), a popup appears.
  10. 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.
That is basically it. You will probably want to do some additional stuff like conditional highlighting or adding a selection mark in front of the menu item. To do this kind of stuff, look up the guide for Event handling in Cocoa. Look specifically for "validateMenuItem" and "menuNeedsUpdate".

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.

Happy coding

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.

No comments:

Post a Comment