Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/AppKit.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSMenu.h |
Related sample code |
This class defines an object that manages an application’s menus.
– insertItem:atIndex:
– insertItemWithTitle:action:keyEquivalent:atIndex:
– addItem:
– addItemWithTitle:action:keyEquivalent:
– removeItem:
– removeItemAtIndex:
– itemChanged:
– indexOfItem:
– indexOfItemWithTitle:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithRepresentedObject:
– indexOfItemWithSubmenu:
– setSubmenu:forItem:
– submenuAction:
– attachedMenu
– isAttached
– locationForSubmenu:
– supermenu
– setSupermenu:
– isTornOff
– menuChangedMessagesEnabled
– setMenuChangedMessagesEnabled:
– sizeToFit
– menu:updateItem:atIndex:shouldCancel:
delegate method
+ popUpContextMenu:withEvent:forView:
+ popUpContextMenu:withEvent:forView:withFont:
– helpRequested:
– highlightedItem
– menu:willHighlightItem:
delegate method
– menuWillOpen:
delegate method
– menuDidClose:
delegate method
– numberOfItemsInMenu:
delegate method
– menuNeedsUpdate:
delegate method
– cancelTracking
– contextMenuRepresentation
– setContextMenuRepresentation:
– tearOffMenuRepresentation
– setTearOffMenuRepresentation:
– setMenuRepresentation:
– menuRepresentation
Returns a Boolean value that indicates whether the menu bar is visible.
+ (BOOL)menuBarVisible
YES
if the menu bar is visible, otherwise NO
.
NSMenu.h
Returns the zone from which NSMenu
objects should be allocated.
+ (NSZone *)menuZone
The zone from which NSMenu
objects should be allocated.
The zone is created if necessary.
NSMenu.h
Displays a contextual menu over a view for an event.
+ (void)popUpContextMenu:(NSMenu *)menu withEvent:(NSEvent *)event forView:(NSView *)view
The menu object to use for the contextual menu.
An NSEvent
object representing the event.
The view object over which to display the contextual menu.
NSMenu.h
Displays a contextual menu over a view for an event using a specified font.
+ (void)popUpContextMenu:(NSMenu *)menu withEvent:(NSEvent *)event forView:(NSView *)view withFont:(NSFont *)font
The menu object to use for the contextual menu.
An NSEvent
object representing the event.
The view object over which to display the contextual menu.
An NSFont
object representing the font for the contextual menu. If you pass in nil
for the font, the method uses the default font for menu.
NSMenu.h
Sets whether the menu bar is visible and selectable by the user.
+ (void)setMenuBarVisible:(BOOL)visible
YES
if menu bar is to be visible, otherwise NO
.
NSMenu.h
Sets the zone from which NSMenu
objects should be allocated
+ (void)setMenuZone:(NSZone *)zone
The memory zone to set.
NSMenu.h
Adds a menu item to the end of the receiver.
- (void)addItem:(NSMenuItem *)newItem
The menu item (an object conforming to the NSMenuItem
protocol) to add to the menu.
This method invokes insertItem:atIndex:
. Thus, the receiver does not accept the menu item if it already belongs to another menu. After adding the menu item, the receiver updates itself.
NSMenu.h
Creates a new menu item and adds it to the end of the receiver.
- (NSMenuItem *)addItemWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)keyEquiv
A string to be made the title of the menu item.
The action-message selector to assign to the menu item.
A string identifying the key to use as a key equivalent for the menu item. If you do not want the menu item to have a key equivalent, keyEquiv should be an empty string (@""
) and not nil
.
The created menu item (an object conforming to the NSMenuItem
protocol) or nil
if the object couldn't be created.
NSMenu.h
Returns the menu currently attached to the receiver.
- (NSMenu *)attachedMenu
The menu currently attached to the receiver or nil
if there’s no such object.
NSMenu.h
Returns a Boolean value that indicates whether the receiver automatically enables and disables its menu items.
- (BOOL)autoenablesItems
YES
if the receiver automatically enables and disables its menu items (based on the NSMenuValidation
informal protocol), otherwise NO
.
By default, NSMenu
objects autoenable their menu items. See the protocol specification for more information.
NSMenu.h
Dismisses the menu and ends all menu tracking.
- (void)cancelTracking
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (id)contextMenuRepresentation
nil
.
NSMenu.h
Returns the receiver’s delegate.
- (id)delegate
The receiver’s delegate.
NSMenu.h
Overridden by subclasses to implement specialized context-sensitive help behavior.
- (void)helpRequested:(NSEvent *)event
An NSEvent
object representing the event associated with the help request.
Subclasses in their implementation of this method should cause the Help Manager (NSHelpManager
) to display the help associated with the receiver. Never invoke this method directly.
– showContextHelpForObject:locationHint:
(NSHelpManager
)NSMenu.h
Returns the highlighted item in the receiver.
- (NSMenuItem *)highlightedItem
Returns the highlighted item in the receiver, or nil
if no item in the menu is highlighted.
NSMenu.h
Returns the index identifying the location of a specified menu item in the receiver.
- (NSInteger)indexOfItem:(NSMenuItem *)anObject
A menu item—that is an object conforming to the NSMenuItem
protocol.
The integer index of the menu item or, if no such menu item is in the menu, –1.
NSMenu.h
Returns the index of the first menu item in the receiver that has a given represented object.
- (NSInteger)indexOfItemWithRepresentedObject:(id)anObject
A represented object of the receiver.
The integer index of the menu item or, if no such menu item is in the menu, –1.
NSMenu.h
Returns the index of the menu item in the receiver with the given submenu.
- (NSInteger)indexOfItemWithSubmenu:(NSMenu *)anObject
A menu object that is a menu item of the receiver (that is, a submenu).
The integer index of the menu item or, if no such menu item is in the menu, –1.
NSMenu.h
Returns the index of the first menu item in the receiver identified by a tag.
- (NSInteger)indexOfItemWithTag:(NSInteger)aTag
An integer tag associated with the menu item of the receiver.
The integer index of the menu item or, if no such menu item is in the menu, –1.
NSMenu.h
Returns the index of the first menu item in the receiver that has a specified action and target.
- (NSInteger)indexOfItemWithTarget:(id)anObject andAction:(SEL)actionSelector
An object that is set as the target of a menu item of the receiver.
A selector identifying an action method. If actionSelector is NULL
, the first menu item in the receiver that has target anObject is returned
The integer index of the menu item or, if no such menu item is in the menu, –1.
– indexOfItemWithTag:
– indexOfItemWithTitle:
– indexOfItemWithRepresentedObject:
– insertItem:atIndex:
– itemAtIndex:
NSMenu.h
Returns the index of the first menu item in the receiver that has a specified title.
- (NSInteger)indexOfItemWithTitle:(NSString *)aTitle
The title of a menu item in the receiver.
The integer index of the menu item or, if no such menu item is in the menu, –1.
NSMenu.h
Initializes and returns a menu having the specified title and with autoenabling of menu items turned on.
- (id)initWithTitle:(NSString *)aTitle
The title to assign to the receiver.
The initialized NSMenu
object or nil
if the object could not be initialized.
This method is the designated initializer for the class.
NSMenu.h
Inserts a menu item into the receiver at a specific location.
- (void)insertItem:(NSMenuItem *)newItem atIndex:(NSInteger)index
An object conforming to the NSMenuItem
protocol that represents a menu item.
An integer index identifying the location of the menu item in the menu.
This method posts an NSMenuDidAddItemNotification
, allowing interested observers to update as appropriate. This method is a primitive method. All item-addition methods end up calling this method, so this is where you should implement custom behavior on adding new items to a menu in a custom subclass. If the menu item already exists in another menu, it is not inserted and the method raises an exception of type NSInternalInconsistencyException
.
NSMenu.h
Creates and adds a menu item at a specified location in the receiver.
- (NSMenuItem *)insertItemWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)keyEquiv atIndex:(NSInteger)index
A string to be made the title of the menu item.
The action-message selector to assign to the menu item.
A string identifying the key to use as a key equivalent for the menu item. If you do not want the menu item to have a key equivalent, keyEquiv should be an empty string (@""
) and not nil
.
An integer index identifying the location of the menu item in the menu.
The new menu item (an object conforming to the NSMenuItem
protocol) or nil
if the item could not be created
NSMenu.h
Returns a Boolean value that indicates whether the receiver is currently attached to another menu.
- (BOOL)isAttached
YES
if the receiver is currently attached to another menu, otherwise NO
.
NSMenu.h
Returns a Boolean value that indicates whether the receiver is offscreen or attached to another menu (or if it’s the main menu).
- (BOOL)isTornOff
NO
if the receiver is offscreen or attached to another menu (or if it’s the main menu), otherwise YES
.
NSMenu.h
Returns an array containing the receiver’s menu items.
- (NSArray *)itemArray
An array containing the receiver’s menu items.
NSMenu.h
Returns the menu item at a specific location of the receiver.
- (NSMenuItem *)itemAtIndex:(NSInteger)index
An integer index locating a menu item in a menu.
The found menu item (an object conforming to the NSMenuItem
protocol) or nil
if the object couldn't be found.
This method raises an exception if index is out of bounds.
NSMenu.h
Invoked when a menu item is modified visually (for example, its title changes).
- (void)itemChanged:(NSMenuItem *)anObject
The menu item that has visually changed.
This method is not called for changes involving the menu item's action, target, represented object, or tag. Posts an NSMenuDidChangeItemNotification
.
NSMenu.h
Returns the first menu item in the receiver with the specified tag.
- (NSMenuItem *)itemWithTag:(NSInteger)aTag
A numeric tag associated with a menu item.
The found menu item (an object conforming to the NSMenuItem
protocol) or nil
if the object couldn't be found.
NSMenu.h
Returns the first menu item in the receiver with a specified title.
- (NSMenuItem *)itemWithTitle:(NSString *)aString
The title of a menu item.
The found menu item (an object conforming to the NSMenuItem
protocol) or nil
if the object couldn't be found.
NSMenu.h
Returns the location in screen coordinates where the given submenu is displayed when opened as a submenu of the receiver.
- (NSPoint)locationForSubmenu:(NSMenu *)aSubmenu
A menu object that is a submenu of the receiver.
An NSPoint
structure describing the location or (0.0, 0.0) if the submenu does not exist in the receiver.
NSMenu.h
Returns the menu bar height for the current application’s main menu.
- (CGFloat)menuBarHeight
The receiver's main menu bar height or 0.0 if the receiver is some other menu.
This method supersedes the menuBarHeight
class method of the NSMenuView
class.
NSMenu.h
Returns a Boolean value that indicates whether messages are sent to the application’s windows upon each change to the receiver.
- (BOOL)menuChangedMessagesEnabled
YES
if messages are sent to the application’s windows upon each change to the receiver, otherwise NO
.
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (id)menuRepresentation
nil
.
NSMenu.h
Returns the number of menu items in the receiver, including separator items.
- (NSInteger)numberOfItems
The number of menu items in the receiver, including separator items.
NSMenu.h
Causes the application to send the action message of a specified menu item to its target.
- (void)performActionForItemAtIndex:(NSInteger)index
The integer index of a menu item.
If a target is not specified, the message is sent to the first responder. As a side effect, this method posts NSMenuWillSendActionNotification
and NSMenuDidSendActionNotification
.
NSMenu.h
Performs the action for the menu item that corresponds to the given key equivalent.
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
An NSEvent
object that represents a key-equivalent event.
YES
if theEvent is a key equivalent that the receiver handled, NO
if it is not a key equivalent that it should handle.
NSMenu.h
Removes a menu item from the receiver.
- (void)removeItem:(NSMenuItem *)anItem
The menu item to remove.
NSMenu.h
Removes the menu item at a specified location in the receiver.
- (void)removeItemAtIndex:(NSInteger)index
An integer index identifying the menu item.
After it removes the menu item, this method posts an NSMenuDidRemoveItemNotification
.
NSMenu.h
Controls whether the receiver automatically enables and disables its menu items based on delegates implementing the NSMenuValidation
informal protocol.
- (void)setAutoenablesItems:(BOOL)flag
If flag is YES
, menu items are automatically enabled and disabled. If flag is NO
, menu items are not automatically enabled or disabled.
See the NSMenuValidation
protocol specification for more information.
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (void)setContextMenuRepresentation:(id)menuRep
NSMenu.h
Sets the receiver’s delegate.
- (void)setDelegate:(id)anObject
The object to set as delegate.
You can use the delegate to populate a menu just before it is going to be drawn and to check for key equivalents without creating a menu item.
NSMenu.h
Controls whether the receiver sends messages to the application’s windows upon each menu change.
- (void)setMenuChangedMessagesEnabled:(BOOL)flag
YES
if the receiver should send a message at each menu change, NO
otherwise.
To avoid the “flickering” effect of many successive menu changes, invoke this method with flag set to NO
, make changes to the menu, and invoke the method again with flag set to YES
. This approach has the effect of batching changes and applying them all at once.
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (void)setMenuRepresentation:(id)menuRep
NSMenu.h
Sets whether the receiver displays the state column.
- (void)setShowsStateColumn:(BOOL)showsState
YES
to display the state column, otherwise NO
.
NSMenu.h
Assigns a menu to be a submenu of the receiver controlled by a given menu item.
- (void)setSubmenu:(NSMenu *)aMenu forItem:(NSMenuItem *)anItem
A menu object that is to be a submenu of the receiver.
A menu item (that is, an object conforming to the NSMenuItem
protocol) that controls aMenu. The method sets the action of anItem to submenuAction:
.
NSMenu.h
Sets the receiver’s supermenu.
- (void)setSupermenu:(NSMenu *)supermenu
A menu object to set as the supermenu of the receiver.
You should never invoke this method directly; it is public so subclassers can add behavior to the default implementation. Subclassers should call the superclass’s method as part of their implementation.
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (void)setTearOffMenuRepresentation:(id)menuRep
NSMenu.h
Sets the receiver’s title.
- (void)setTitle:(NSString *)aString
A string to assign as the new title of the receiver.
NSMenu.h
Returns a Boolean value that indicates whether the receiver displays the state column.
- (BOOL)showsStateColumn
YES
if the receiver displays the state column, otherwise NO
.
NSMenu.h
Resizes the receiver to exactly fit its items.
- (void)sizeToFit
NSMenu.h
The action method assigned to menu items that open submenus.
- (void)submenuAction:(id)sender
You may override this method to implement different behavior. Never invoke this method directly.
NSMenu.h
Returns the receiver’s supermenu.
- (NSMenu *)supermenu
The receiver’s supermenu or nil
if it has none.
NSMenu.h
Deprecated. (Deprecated. Mac OS X does not use menu representations to draw menus.)
- (id)tearOffMenuRepresentation
nil
.
NSMenu.h
Returns the receiver’s title.
- (NSString *)title
The receiver’s title.
NSMenu.h
Enables or disables the receiver’s menu items based on the NSMenuValidation
informal protocol and sizes the menu to fit its current menu items if necessary.
- (void)update
See the NSMenuValidation
protocol specification for more information.
NSMenu.h
Called to let the delegate update a menu item before it is displayed.
- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
The menu object that owns item
.
The menu-item object that may be updated.
The integer index of the menu item.
Set to YES
if, due to some user action, the menu no longer needs to be displayed before all the menu items have been updated. You can ignore this flag, return YES
, and continue; or you can save your work (to save time the next time your delegate is called) and return NO
to stop the updating.
YES
to continue the process. If you return NO
, your menu:updateItem:atIndex:shouldCancel:
is not called again. In that case, it is your responsibility to trim any extra items from the menu.
If your numberOfItemsInMenu:
delegate method returns a positive value, then your menu:updateItem:atIndex:shouldCancel:
method is called for each item in the menu. You can then update the menu title, image, and so forth for each menu item.
NSMenu.h
Called to indicates that a menu is about to highlight a given item.
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
The menu object about to highlight an item.
The item about to be highlighted.
Only one item per menu can be highlighted at a time. If item is nil
, it means that all items in the menu are about to be unhighlighted
NSMenu.h
Sent after a menu closed.
- (void)menuDidClose:(NSMenu *)menu
The menu that closed.
Do not modify the structure of the menu or the menu items during this method.
NSMenu.h
Called to allow the delegate to return the target and action for a key-down event.
- (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action
The menu object sending the delegation message.
An NSEvent
object representing a key-down event.
Return by reference the target object for the menu item that corresponds to the event. Specify nil
to requests the menu's target.
Return by reference the action selector for the menu item that corresponds to the event.
If there is a valid and enabled menu item that corresponds to this key-down even, return YES
after specifying the target and action. Return NO
if there are no items with that key equivalent or if the item is disabled.
If the delegate does not define this method, the menu is populated to find out if any items have a matching key equivalent.
NSMenu.h
Called when a menu is about to be displayed at the start of a tracking session so the delegate can modify the menu.
- (void)menuNeedsUpdate:(NSMenu *)menu
The menu object that is about to be displayed.
You can change the menu by adding, removing or modifying menu items. Be sure to set the proper enable state for any new menu items. If populating the menu will take a long time, implement numberOfItemsInMenu:
and menu:updateItem:atIndex:shouldCancel:
instead.
NSMenu.h
Sent when a menu is about to open.
- (void)menuWillOpen:(NSMenu *)menu
The menu that is about to open.
Do not modify the structure of the menu or the menu items during this method.
NSMenu.h
Called when a menu is about to be displayed at the start of a tracking session so the delegate can specify the number of items in the menu.
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
The menu object about to be displayed.
The number of menu items in the menu.
If you return a positive value, the menu is resized by either removing or adding items. Newly created items are blank. After the menu is resized, your menu:updateItem:atIndex:shouldCancel:
method is called for each item. If you return a negative value, the number of items is left unchanged and menu:updateItem:atIndex:shouldCancel:
is not called. If you can populate the menu quickly, you can implement menuNeedsUpdate:
instead of numberOfItemsInMenu:
and menu:updateItem:atIndex:shouldCancel:
.
NSMenu.h
Posted after a menu item is added to the menu. The notification object is the instance of NSMenu
that just added the new menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
An |
NSMenu.h
Posted after a menu item in the menu changes appearance. Changes include enabling/disabling, changes in state, and changes to title. The notification object is the instance of NSMenu
with the menu item that changed. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
An |
NSMenu.h
Posted when menu tracking begins. The notification object is the main menu bar ([NSApp mainMenu]
) or the root menu of a popup button. This notification does not contain a userInfo dictionary.
Note: This notification is available in versions 10.3 and 10.4 of Mac OS X, however it is not publicly declared so you must declare the name constant as an extern
, for example:
extern NSString *NSMenuDidBeginTrackingNotification; |
NSMenu.h
Posted when menu tracking ends, even if no action is sent. The notification object is the main menu bar ([NSApp mainMenu]
) or the root menu of a popup button. This notification does not contain a userInfo dictionary.
NSMenu.h
Posted after a menu item is removed from the menu. The notification object is the instance of NSMenu
that just removed the menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
An |
NSMenu.h
Posted just after the application dispatches a menu item’s action method to the menu item’s target. The notification object is the instance of NSMenu
containing the chosen menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The menu item that was chosen. |
NSMenu.h
Posted just before the application dispatches a menu item’s action method to the menu item’s target. The notification object is the instance of NSMenu
containing the chosen menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The menu item that was chosen. |
NSMenu.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)