Legacy Mac OS X Reference Library Apple Developer Connection

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.


NSMenuItem Protocol Reference (Not Recommended)

Adopted by
NSMenuItem
Conforms to
NSObject
NSCopying
NSCoding
NSValidatedUserInterfaceItem
Framework
/System/Library/Frameworks/AppKit.framework
Availability
Available in Mac OS X v10.0 through Mac OS X v10.4.
Companion guide
Application Menu and Pop-up List Programming Topics for Cocoa
Declared in
NSMenuItem.h

Overview

Important: The information in this document is obsolete and should not be used for new development.

Refer to the NSMenuItem class description, which replaces this protocol.

! icon

Warning:  The NSMenuItem protocol is being removed from the Application Kit; you must use the NSMenuItem class instead. This change does not affect binary compatibility between different versions of projects, but might cause failures in project builds. To adapt your projects to this change, alter all references to the protocol (for example, id <NSMenuItem>) to references to the class (NSMenuItem *).

Tasks

Creating a Menu Item

Enabling a Menu Item

Setting the Target and Action

Setting the Title

Setting the Tag

Setting the State

Setting the Image

Managing Submenus

Getting a Separator Item

Setting the Owning Menu

Managing Key Equivalents

Managing Mnemonics

Managing User Key Equivalents

Managing Alternates

Managing Indentation Levels

Managing Tool Tips

Representing an Object

Class Methods

separatorItem

Returns a menu item that is used to separate logical groups of menu commands. (required)

+ (id <NSMenuItem>)separatorItem

Discussion

This menu item is disabled. The default separator item is blank space.

setUsesUserKeyEquivalents:

Sets whether menu items conform to user preferences for key equivalents. (required)

+ (void)setUsesUserKeyEquivalents:(BOOL)flag

Parameters
flag

If YES, menu items conform to user preferences for key equivalents; if NO, the key equivalents originally assigned to the menu items are used.

See Also

usesUserKeyEquivalents

Returns YES if menu items conform to user preferences for key equivalents; otherwise, returns NO. (required)

+ (BOOL)usesUserKeyEquivalents

See Also

Instance Methods

action

Returns the receiver’s action method. (required)

- (SEL)action

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

attributedTitle

Returns the custom title string for a menu item. (required)

- (NSAttributedString *)attributedTitle

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

hasSubmenu

Returns YES if the receiver has a submenu, NO if it doesn’t. (required)

- (BOOL)hasSubmenu

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

image

Returns the image displayed by the receiver, or nil if it displays no image. (required)

- (NSImage *)image

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

indentationLevel

Returns the menu item indentation level for the receiver. (required)

- (NSInteger)indentationLevel

Discussion

The return value will be from 0 to 15. The default indentation level is 0.

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

initWithTitle:action:keyEquivalent:

Returns an initialized instance of an NSMenuItem. (required)

- (id)initWithTitle:(NSString *)itemName action:(SEL)anAction keyEquivalent:(NSString *)charCode

Parameters
itemName

The title of the menu item. It must not be nil (if there is no title, specify an empty NSString).

anAction

The action selector to be associated with the menu item. It must be a valid selector or NULL.

charCode

A string representing a keyboard key to be used as the key equivalent. It must not be nil (if there is no key equivalent, specify an empty NSString).

Return Value

Returns an instance of NSMenuItem or nil if the object couldn't be created.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared In
NSMenuItem.h

isAlternate

Returns whether the receiver is an alternate to the previous menu item. (required)

- (BOOL)isAlternate

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

isEnabled

Returns YES if the receiver is enabled, NO if not. (required)

- (BOOL)isEnabled

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

isSeparatorItem

Returns whether the receiver is a separator item (that is, a menu item used to visually segregate related menu items). (required)

- (BOOL)isSeparatorItem

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared In
NSMenuItem.h

keyEquivalent

Returns the receiver’s unmodified keyboard equivalent, or the empty string if one hasn’t been defined. (required)

- (NSString *)keyEquivalent

Discussion

Use keyEquivalentModifierMask to determine the modifier mask for the key equivalent.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

keyEquivalentModifierMask

Returns the receiver’s keyboard equivalent modifier mask. (required)

- (NSUInteger)keyEquivalentModifierMask

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

menu

Returns the menu to which the receiver belongs, or nil if no menu has been set. (required)

- (NSMenu *)menu

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

mixedStateImage

Returns the image used to depict a “mixed state.” (required)

- (NSImage *)mixedStateImage

Discussion

A mixed state is useful for indicating “off” and “on” attribute values in a group of selected objects, such as a selection of text containing bold and plain (nonbolded) words.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

mnemonic

Returns the character in the menu item title that appears underlined for use as a mnemonic. (required)

- (NSString *)mnemonic

Discussion

If there is no mnemonic character, returns an empty string. Mnemonics are not supported in Mac OS X.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

mnemonicLocation

Returns the position of the underlined character in the menu item title used as a mnemonic. (required)

- (NSUInteger)mnemonicLocation

Discussion

The position is the zero-based index of that character in the title string. If the receiver has no mnemonic character, returns NSNotFound. Mnemonics are not supported in Mac OS X.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

offStateImage

Returns the image used to depict the receiver’s “off” state, or nil if the image has not been set. (required)

- (NSImage *)offStateImage

Discussion

By default, there is no off state image.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

onStateImage

Returns the image used to depict the receiver’s “on” state, or nil if the image has not been set. (required)

- (NSImage *)onStateImage

Discussion

By default, the on state image is a checkmark.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

representedObject

Returns the object that the receiving menu item represents. (required)

- (id)representedObject

Discussion

For example, you might have a menu list the names of views that are swapped into the same panel. The represented objects would be the appropriate NSView objects. The user would then be able to switch back and forth between the different views that are displayed by selecting the various menu items.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setAction:

Sets the receiver’s action method. (required)

- (void)setAction:(SEL)aSelector

Parameters
aSelector

A selector identifying the action method.

Discussion

See Action Messages for additional information on action messages.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setAlternate:

Marks the receiver as an alternate to the previous menu item. (required)

- (void)setAlternate:(BOOL)isAlternate

Parameters
isAlternate

YES if the receiver is an alternate to the previous menu item, NO otherwise.

Discussion

If the receiver has the same key equivalent as the previous item, but has different key equivalent modifiers, the items are folded into a single visible item and the appropriate item shows while tracking the menu. The menu items may also have no key equivalent as long as the key equivalent modifiers are different.

If there are two or more items with no key equivalent but different modifiers, then the only way to get access to the alternate items is with the mouse. If you mark items as alternates but their key equivalents don’t match, they might be displayed as separate items. Marking the first item as an alternate has no effect.

The isAlternate value is archived.

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

setAttributedTitle:

Specifies a custom string for a menu item. (required)

- (void)setAttributedTitle:(NSAttributedString *)string

Parameters
string

An attributed string to use as the receiver's title.

Discussion

You can use this method to add styled text and embedded images to menu item strings. If you do not set a text color for the attributed string, it is black when not selected, white when selected, and gray when disabled. Colored text remains unchanged when selected.

When you call this method to set the menu title to an attributed string, the setTitle: method is also called to set the menu title with a plain string. If you clear the attributed title, the plain title remains unchanged.

The attributed string is not archived in the old nib format.

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

setEnabled:

Sets whether the receiver is enabled. (required)

- (void)setEnabled:(BOOL)flag

Parameters
flag

YES if the receiver is to be enabled, NO otherwise.

Discussion

If a menu item is disabled, its keyboard equivalent is also disabled. See the NSMenuValidation informal protocol specification for cautions regarding this method.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setImage:

Sets the receiver’s image. (required)

- (void)setImage:(NSImage *)menuImage

Parameters
menuImage

An NSImage object representing an image to be displayed in the menu item. If menuImage is nil, the current image (if any) is removed.

Discussion

The menu item's image is not affected by changes in its state.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setIndentationLevel:

Sets the menu item indentation level for the receiver. (required)

- (void)setIndentationLevel:(NSInteger)indentationLevel

Parameters
indentationLevel

The value for indentationLevel may be from 0 to 15. If indentationLevel is greater than 15, the value is pinned to the maximum. If indentationLevel is less than 0, an exception is raised. The default indentation level is 0.

Discussion

The indentationLevel value is archived.

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

setKeyEquivalent:

Sets the receiver’s unmodified key equivalent. (required)

- (void)setKeyEquivalent:(NSString *)aKeyEquivalent

Parameters
aKeyEquivalent

A string containing a character code representing a keyboard key. If you want to remove the key equivalent from a menu item, pass an empty string (@"") for aString (never pass nil).

Discussion

Use setKeyEquivalentModifierMask: to set the appropriate mask for the modifier keys for the key equivalent.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setKeyEquivalentModifierMask:

Sets the receiver’s keyboard equivalent modifiers (indicating modifiers such as the Shift or Option key) to those in mask. (required)

- (void)setKeyEquivalentModifierMask:(NSUInteger)mask

Parameters
mask

The key masks indicate modifiers such as the Shift or Option keys. mask is an integer bit field containing any of these modifier key masks, combined using the C bitwise OR operator:

  • NSShiftKeyMask

  • NSAlternateKeyMask

  • NSCommandKeyMask

  • NSControlKeyMask

You should always set NSCommandKeyMask in mask.

NSShiftKeyMask is relevant only for function keys—that is, for key events whose modifier flags include NSFunctionKeyMask. For all other key events NSShiftKeyMask is ignored, and characters typed while the Shift key is pressed are interpreted as the shifted versions of those characters; for example, Command-Shift-c is interpreted as Command-C.

Discussion

See the NSEvent class specification for more information about modifier mask values.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setMenu:

Sets the receiver’s menu. (required)

- (void)setMenu:(NSMenu *)aMenu

Parameters
aMenu

The menu object that "owns" the receiver.

Discussion

This method is invoked by the owning NSMenu object when the receiver is added or removed. You shouldn’t have to invoke this method in your own code, although it can be overridden to provide specialized behavior.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setMixedStateImage:

Sets the image of the receiver that indicates a “mixed” state, that is, a state neither “on” nor “off.” (required)

- (void)setMixedStateImage:(NSImage *)itemImage

Parameters
itemImage

The NSImage object to use for the "mixed" state of the menu item. If itemImage is nil, any current mixed-state image is removed.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setMnemonicLocation:

Sets the character of the specified menu item that is to be underlined. (required)

- (void)setMnemonicLocation:(NSUInteger)location

Parameters
location

An integer index into the character array of the title.

Discussion

This character identifies the access key by which users can access the menu item. Mnemonics are not supported in Mac OS X.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setOffStateImage:

Sets the image of the receiver that indicates an “off” state. (required)

- (void)setOffStateImage:(NSImage *)itemImage

Parameters
itemImage

The NSImage object to use for the "off" state of the menu item. If itemImage is nil, any current off-state image is removed.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setOnStateImage:

Sets the image of the receiver that indicates an “on” state. (required)

- (void)setOnStateImage:(NSImage *)itemImage

Parameters
itemImage

The NSImage object to use for the "on" state of the menu item. If itemImage is nil, any current on-state image is removed.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setRepresentedObject:

Sets the object represented by the receiver. (required)

- (void)setRepresentedObject:(id)anObject

Parameters
anObject

The object to be represented by the receiver.

Discussion

By setting a represented object for a menu item, you make an association between the menu item and that object. The represented object functions as a more specific form of tag that allows you to associate any object, not just an arbitrary integer, with the items in a menu.

For example, an NSView object might be associated with a menu item—when the user chooses the menu item, the represented object is fetched and displayed in a panel. Several menu items might control the display of multiple views in the same panel.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setState:

Sets the state of the receiver. (required)

- (void)setState:(NSInteger)itemState

Parameters
itemState

An integer constant representing a state; it should be one of NSOffState, NSOnState, or NSMixedState.

Discussion

The image associated with the new state is displayed to the left of the menu item.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setSubmenu:

Sets the submenu of the receive. (required)

- (void)setSubmenu:(NSMenu *)aSubmenu

Parameters
aSubmenu

The menu object to set as submenu.

Discussion

The default implementation raises an exception if aSubmenu already has a supermenu.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setTag:

Sets the receiver’s tag. (required)

- (void)setTag:(NSInteger)anInt

Parameters
anInt

An integer tag to associate with the receiver.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setTarget:

Sets the receiver’s target. (required)

- (void)setTarget:(id)anObject

Parameters
anObject

An object to be the target of action messages sent by the receiver.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setTitle:

Sets the receiver’s title. (required)

- (void)setTitle:(NSString *)aString

Parameters
aString

The new title of the menu item. If you do not want a title, use an empty string, not nil.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setTitleWithMnemonic:

Sets the title of a menu item with a character underlined to denote an access key. (required)

- (void)setTitleWithMnemonic:(NSString *)aString

Discussion

Mnemonics are not supported in Mac OS X.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

setToolTip:

Sets a help tag for a menu item. (required)

- (void)setToolTip:(NSString *)toolTip

Parameters
toolTip

A short string that describes the menu item.

Discussion

You can invoke this method for any menu item, including items in the main menu bar. This string is not archived in the old nib format.

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

state

Returns the state of the receiver. (required)

- (NSInteger)state

Discussion

A menu-item state can be one of NSOffState (the default), NSOnState, or NSMixedState.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

submenu

Returns the submenu associated with the receiving menu item, or nil if no submenu is associated with it. (required)

- (NSMenu *)submenu

Discussion

If the receiver responds YES to hasSubmenu, the submenu is returned.

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

tag

Returns the receiver’s tag. (required)

- (NSInteger)tag

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

target

Returns the receiver’s target. (required)

- (id)target

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

title

Returns the receiver’s title. (required)

- (NSString *)title

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
See Also
Declared In
NSMenuItem.h

toolTip

Returns the help tag for a menu item. (required)

- (NSString *)toolTip

Availability
  • Available in Mac OS X v10.3 and later.
See Also
Declared In
NSMenuItem.h

userKeyEquivalent

Returns the user-assigned key equivalent for the receiver. (required)

- (NSString *)userKeyEquivalent

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared In
NSMenuItem.h

userKeyEquivalentModifierMask

Returns the modifier mask for the receiver’s user-assigned key equivalent. (required)

- (NSUInteger)userKeyEquivalentModifierMask

Availability
  • Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared In
NSMenuItem.h


Last updated: 2007-02-08

Did this document help you? Yes It's good, but... Not helpful...