osx

NSMenuItem

Remarks#

See Apple’s Documentation here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/

Enabling menu items

Manually enabling menu items

To manually control the enabled state of a menu items the menu that contains it must disable automatic enabling of its items

Menus can turn off automatic enabling in one of two ways:

  1. In the Interface Builder

Interface Builder assistant editor screenshot showing Auto Enables Items disabled

  1. In code

menu.autoenablesItems = false

Both of the mechanisms set the autoenablesItems property on NSMenu.

Once the menu is menu is no longer enabling and disabling menu items the menu items can be programmatically set in one of two ways

  1. In Interface Builder
  2. In code
menuItem.enabled = true

Automatically enabling menu items

Menu items can be automatically enabled by connecting menu items actions to the first responder and implementing the delivered action on an object in the responder chain as described by Apple’s docs.

Supporting default menu actions

Menus act like all standard control items. They have an action which is the function to be called and a target which is the object to send the function to. If the target is set to an object then when a user selects a menu item it the action method will be sent to the target object. If the menu item has an action, but not a target then the target will be dynamically selected from the first object from the following that responds to the action:

  1. The first responder
  2. The view hierarchy
  3. Window
  4. Window controller
  5. NSApplication
  6. NSApplication.delegate
  7. NSApplication.nextResponder

Implementing the default Open (⌘O) menu item can be accomplished by implementing the openDocument method on any object in the above list.


- (IBAction)openDocument:(id)sender {

}

Adding and removing items to a menu

// add an item to a menu 
menu.addItem(item)

// remove and item from a menu
menu.removeItem(item)

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow