Connecting Menu Items to IBActions in a Mac Storyboard

February 23rd, 2015

Filed under: Cocoa, Interface Builder, Mac Development | 6 comments

A common pattern in Mac application development is to create a menu item, create an IBAction for that item in one of your classes, and connect the menu item to the IBAction. When the person running your application chooses that menu item, your application performs the IBAction.

If you’re using storyboards to develop your Mac application, you’re probably placing your IBActions in your view controllers. When trying to connect a menu item to an IBAction, your first instinct is to make a connection from the menu item to the view controller containing the IBAction. But if you make the connection, you’ll see the following HUD:

ActionSegueHUD

That’s not what you were expecting to see. How do you connect the menu item so you can wire up your IBAction? Make the connection from the menu item to the application scene’s First Responder.

ApplicationSceneFirstResponder

When you make the connection to the application scene’s First Responder, the HUD will show a long list of actions instead of segues. Your IBActions should appear in that list.

Tags:


6 thoughts on “Connecting Menu Items to IBActions in a Mac Storyboard

  1. Joseph Baraga says:

    I have done this for the open menu item under the file menu. However, when I run the app the open menu item remains inactive and greyed out. What else has to be done?

    • Mark Szymczyk says:

      Joseph,

      The Open menu item is meant to be used only with the default openDocument IBAction.

      I did a test where I changed the Open menu item to use the open IBAction instead of the default openDocument IBAction. The Open menu item was grayed out when I ran the application. If the Open menu is grayed out for Apple-supplied IBActions, I don’t know how you would enable the Open menu for IBActions you write.

  2. Joseph Baraga says:

    Thanks for the quick reply.
    It does work if I connect it to my own IB action in the app delegate, but nowhere else. Maybe I will have to make my own open menu item, but I’m not sure how to do this.

  3. Joseph Baraga says:

    I did finally get it to work with my own “Open” menu item. Occasionally the menu item will be inactive when I run, this can be corrected by choosing the close menu item ( even though I have not wired that up), and re-running. I would guess that’s an Xcode bug.

    It’s surprising how difficult this is to do, given the apparent preference by Apple to use storyboards and view controllers. It should be a lot easier and more obvious.

    • Mark Szymczyk says:

      Joseph,

      I’m glad you got things working.

      What I find most difficult about working with OS X storyboards is the lack of documentation from Apple. They don’t even answer Mac storyboard questions on their developer forums and mailing lists. I filed a bug report asking for OS X storyboard documentation in October, and Apple hasn’t responded to it. I’m hoping for some Mac storyboard documentation to be introduced at WWDC.

  4. John Joyce says:

    Unfortunately, what you’re running up against is ages of semi-tribal knowledge where things have been documented, but not consistently in one place, over time.
    Menu bar menu actions were always supposed to go in FirstResponder. The old approach was to build out all menu actions first. Then wire up UI work.
    So how do you get those grayed out menu items to know they have actions?
    Well, something in the responder chain (which is a lot less relied upon on iOS) needs to implement the protocols that do ‘validation’. Specifically NSMenuValidation Protocol https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSMenuValidation_Protocol/index.html

    You, see, Objective-C is very dynamic, and does things that Swift would never have brought about, the responder chain is one, where a message is sent through the responder chain, anybody in the chain can handle it, and if they do they change some state along the way, such as with menu validation. The advantage is such that the logic can be purely event driven and the interface changes contextually.

Leave a Reply to Joseph Baraga Cancel reply

Your email address will not be published. Required fields are marked *