NSPopUpButton Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AppKit.framework
Availability
Available in OS X v10.0 and later.
Companion guide
Declared in
NSPopUpButton.h
Related sample code

Overview

The NSPopUpButton class defines objects that implement the pop-up and pull-down menus of the graphical user interface.

An NSPopUpButton object uses an NSPopUpButtonCell object to implement its user interface.

Note that while a menu is tracking user input, programmatic changes to the menu such as adding, removing, or changing items on the menu is not reflected.

Tasks

Initializing an NSPopUpButton

Setting the Type of Menu

Inserting and Deleting Items

Getting the User’s Selection

Setting the Current Selection

Getting Menu Items

Getting the Indices of Menu Items

Setting the Cell Edge to Pop out in Restricted Situations

Setting the Title

Setting the Image

Setting the State

Instance Methods

addItemsWithTitles:

Adds multiple items to the end of the menu.

- (void)addItemsWithTitles:(NSArray *)itemTitles
Parameters
itemTitles

An array of NSString objects containing the titles of the items you want to add. Each string in the array should be unique. If an item with the same title already exists in the menu, the existing item is removed and the new one is added.

Discussion

If you want to move an item, it’s better to invoke removeItemWithTitle: explicitly and then send this method. After adding the items, this method uses the synchronizeTitleAndSelectedItem method to make sure the item being displayed matches the currently selected item.

Since this method searches for duplicate items, it should not be used if you are adding items to an already populated menu with more than a few hundred items. Add items directly to the receiver's menu instead.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

addItemWithTitle:

Adds an item with the specified title to the end of the menu.

- (void)addItemWithTitle:(NSString *)title
Parameters
title

The title of the menu-item entry. If an item with the same title already exists in the menu, the existing item is removed and the new one is added.

Discussion

If you want to move an item, it’s better to invoke removeItemWithTitle: explicitly and then send this method. After adding the item, this method calls the synchronizeTitleAndSelectedItem method to make sure the item being displayed matches the currently selected item.

Since this method searches for duplicate items, it should not be used if you are adding an item to an already populated menu with more than a few hundred items. Add items directly to the receiver's menu instead.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

autoenablesItems

Returns whether the receiver automatically enables and disables its items every time a user event occurs.

- (BOOL)autoenablesItems
Return Value

YES if the receiver automatically enables and disables items; otherwise, NO. The default value is YES.

Discussion

For more information on enabling and disabling menu items, see the NSMenuValidation protocol specification.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfItem:

Returns the index of the specified menu item.

- (NSInteger)indexOfItem:(NSMenuItem *)anObject
Parameters
anObject

The menu item whose index you want.

Return Value

The index of the item or -1 if no such item was found.

Discussion

This method invokes the method of the same name of its NSPopUpButtonCell object.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfItemWithRepresentedObject:

Returns the index of the menu item that holds the specified represented object.

- (NSInteger)indexOfItemWithRepresentedObject:(id)anObject
Parameters
anObject

The represented object associated with a menu item.

Return Value

The index of the menu item that owns the specified object, or -1 if no such menu item was found.

Discussion

Represented objects bear some direct relation to the title or image of a menu item; for example, an item entitled “100” might have an NSNumber object encapsulating that value as its represented object. This method invokes the method of the same name of its NSPopUpButtonCell object.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfItemWithTag:

Returns the index of the menu item with the specified tag.

- (NSInteger)indexOfItemWithTag:(NSInteger)tag
Parameters
tag

The tag of the menu item you want.

Return Value

The index of the item or -1 if no item with the specified tag was found.

Discussion

This method invokes the method of the same name of its NSPopUpButtonCell object.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfItemWithTarget:andAction:

Returns the index of the menu item with the specified target and action.

- (NSInteger)indexOfItemWithTarget:(id)target andAction:(SEL)actionSelector
Parameters
target

The target object associated with the menu item.

actionSelector

The action method associated with the menu item.

Return Value

The index of the menu item, or -1 if no menu item contains the specified target and action.

Discussion

If you specify NULL for the actionSelector parameter, the index of the first menu item with the specified target is returned. This method invokes the method of the same name of its NSPopUpButtonCell object.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfItemWithTitle:

Returns the index of the item with the specified title.

- (NSInteger)indexOfItemWithTitle:(NSString *)title
Parameters
title

The title of the item you want.

Return Value

The index of the item or -1 if no item with the specified title was found.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

indexOfSelectedItem

Returns the index of the item last selected by the user.

- (NSInteger)indexOfSelectedItem
Return Value

The index of the selected item, or -1 if no item is selected.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

initWithFrame:pullsDown:

Returns an NSPopUpButton object initialized to the specified dimensions.

- (id)initWithFrame:(NSRect)frameRect pullsDown:(BOOL)flag
Parameters
frameRect

The frame rectangle for the button, specified in the parent view's coordinate system.

flag

YES if you want the receiver to display a pull-down menu; otherwise, NO if you want it to display a pop-up menu.

Return Value

An initialized NSPopUpButton object, or nil if the object could not be initialized.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

insertItemWithTitle:atIndex:

Inserts an item at the specified position in the menu.

- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index
Parameters
title

The title of the new item. If an item with the same title already exists in the menu, the existing item is removed and the new one is added

index

The zero-based index at which to insert the item. Specifying 0 inserts the item at the top of the menu.

Discussion

If you want to move an item, it’s better to invoke removeItemWithTitle: explicitly and then send this method. After adding the item, this method uses the synchronizeTitleAndSelectedItem method to make sure the item displayed matches the currently selected item.

Since this method searches for duplicate items, it should not be used if you are adding an item to an already populated menu with more than a few hundred items. Add items directly to the receiver's menu instead.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

itemArray

Returns the items in the menu.

- (NSArray *)itemArray
Return Value

An array of NSMenuItem objects representing the items in the menu.

Discussion

Usually you access the menu’s items and modify the menu using the methods of NSPopUpButton rather than accessing the array of items directly.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

itemAtIndex:

Returns the menu item at the specified index.

- (NSMenuItem *)itemAtIndex:(NSInteger)index
Parameters
index

The index of the item you want.

Return Value

The menu item, or nil if no item exists at the specified index.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

itemTitleAtIndex:

Returns the title of the item at the specified index.

- (NSString *)itemTitleAtIndex:(NSInteger)index
Parameters
index

The index of the item you want.

Return Value

The title of the item, or an empty string if no item exists at the specified index.

Availability
  • Available in OS X v10.0 and later.
See Also
Related Sample Code
Declared In
NSPopUpButton.h

itemTitles

Returns the titles of all of the items in the menu.

- (NSArray *)itemTitles
Return Value

An array of NSString objects containing the titles of every item in the menu. The titles appear in the order in which the items appear in the menu.

Discussion

If the menu contains separator items, the array contains an empty string (@””) for each separator item.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

itemWithTitle:

Returns the menu item with the specified title.

- (NSMenuItem *)itemWithTitle:(NSString *)title
Parameters
title

The title of the menu item you want.

Return Value

The menu item, or nil if no item with the specified title exists in the menu.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

lastItem

Returns the last item in the menu.

- (NSMenuItem *)lastItem
Return Value

The last menu item.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

menu

Returns the pop-up button’s associated menu.

- (NSMenu *)menu
Return Value

The menu for the pop-up button.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

numberOfItems

Returns the number of items in the menu.

- (NSInteger)numberOfItems
Return Value

The number of items in the menu.

Availability
  • Available in OS X v10.0 and later.
See Also
Related Sample Code
Declared In
NSPopUpButton.h

objectValue

Returns the index of the selected item.

- (id)objectValue
Return Value

An object (typically an NSNumber object) that responds to the intValue message and contains the index of the selected item.

preferredEdge

Returns the edge of the receiver next to which the pop-up menu is displayed under restrictive screen conditions.

- (NSRectEdge)preferredEdge
Return Value

Possible values include NSMinXEdge, NSMinYEdge, NSMaxXEdge, or NSMaxYEdge. The default value is the bottom edge, which is NSMaxYEdge for flipped views or NSMinYEdge for unflipped views.

Discussion

For pull-down menus, the default behavior is to display the menu under the receiver. For most pop-up menus, the NSPopUpButton object attempts to show the selected item directly over the button.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

pullsDown

Returns a Boolean value indicating the behavior of the control's menu.

- (BOOL)pullsDown
Return Value

YES if the menu behaves like a pull-down menu; otherwise, NO if it behaves like a pop-up menu.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

removeAllItems

Removes all items in the receiver’s item menu.

- (void)removeAllItems
Discussion

After removing the items, this method uses the synchronizeTitleAndSelectedItem method to refresh the menu.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

removeItemAtIndex:

Removes the item at the specified index.

- (void)removeItemAtIndex:(NSInteger)index
Parameters
index

The zero-based index indicating which item to remove. Specifying 0 removes the item at the top of the menu.

Discussion

After removing the item, this method uses the synchronizeTitleAndSelectedItem method to make sure the title displayed matches the currently selected item.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

removeItemWithTitle:

Removes the item with the specified title from the menu.

- (void)removeItemWithTitle:(NSString *)title
Parameters
title

The title of the item you want to remove. If no menu item exists with the specified title, this method triggers an assertion.

Discussion

This method removes the first item it finds with the specified name. This method then uses synchronizeTitleAndSelectedItem to refresh the menu.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

selectedItem

Returns the menu item last selected by the user.

- (NSMenuItem *)selectedItem
Return Value

The menu item that is currently selected, or nil if no item is selected.

Discussion

The last selected menu item is the one that was highlighted when the user released the mouse button. It is possible for a pull-down menu’s selected item to be its first item.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

selectItem:

Selects the specified menu item.

- (void)selectItem:(NSMenuItem *)item
Parameters
anObject

The menu item to select, or nil if you want to deselect all menu items.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

selectItemAtIndex:

Selects the item in the menu at the specified index.

- (void)selectItemAtIndex:(NSInteger)index
Parameters
index

The index of the item you want to select, or -1 you want to deselect all menu items.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

selectItemWithTag:

Selects the menu item with the specified tag.

- (BOOL)selectItemWithTag:(NSInteger)tag
Parameters
tag

The tag of the item you want to select.

Return Value

YES if the item was successfully selected; otherwise, NO.

Discussion

If no item with the specified tag is found, this method returns NO and leaves the menu state unchanged.

You typically assign tags to menu items from Interface Builder, but you can also assign them programmatically using the setTag: method of NSMenuItem.

Availability
  • Available in OS X v10.4 and later.
Related Sample Code
Declared In
NSPopUpButton.h

selectItemWithTitle:

Selects the item with the specified title.

- (void)selectItemWithTitle:(NSString *)title
Parameters
title

The title of the item to select. If you specify nil, an empty string, or a string that does not match the title of a menu item, this method deselects the currently selected item.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

setAutoenablesItems:

Sets whether the receiver automatically enables and disables its items every time a user event occurs.

- (void)setAutoenablesItems:(BOOL)flag
Parameters
flag

YES if you want the receiver to automatically enable and disable items; otherwise, NO.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

setImage:

This method has no effect.

- (void)setImage:(NSImage *)anImage
Parameters
anImage

The image to display.

Discussion

The image displayed in a pop up button cell is taken from the selected menu item (in the case of a pop up menu) or from the first menu item (in the case of a pull-down menu).

setMenu:

Sets the pop-up button’s associated menu .

- (void)setMenu:(NSMenu *)menu
Parameters
menu

The menu to associate with the pop-up button.

Discussion

If another menu was already associated with the pop-up button, this method releases the old menu. If you want to explicitly save the old menu, you should retain it before invoking this method.

Availability
  • Available in OS X v10.0 and later.
Related Sample Code
Declared In
NSPopUpButton.h

setObjectValue:

Selects the item at a specific index using an object value.

- (void)setObjectValue:(id)object
Parameters
object

An NSNumber object containing the index (an integer) of the item you want to select. Specify the index -1 to deselect all items. You can also use an object other than an NSNumber object. In that case, the object must respond to the intValue method and return an appropriate index value.

Discussion

setPreferredEdge:

Sets the edge of the receiver next to which the pop-up menu should appear under restrictive screen conditions.

- (void)setPreferredEdge:(NSRectEdge)edge
Parameters
edge

The preferred edge. Possible values include NSMinXEdge, NSMinYEdge, NSMaxXEdge, or NSMaxYEdge.

Discussion

For pull-down menus, the default behavior is to display the menu under the receiver. For most pop-up menus, the NSPopUpButton object attempts to show the selected item directly over the button.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

setPullsDown:

Sets whether the receiver behaves as a pull-down or pop-up menu.

- (void)setPullsDown:(BOOL)flag
Parameters
flag

YES if you want the receiver to operate as a pull-down menu; otherwise, NO if you want it to operate as a pop-up menu.

Discussion

This method does not change the contents of the menu; it changes only the style of the menu.

When changing the menu type to a pull-down menu, if the menu was a pop-up menu and the cell alters the state of its selected items, this method sets the state of the currently selected item to NSStateOff before changing the menu type.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

setTitle:

Sets the string displayed in the receiver when the user isn’t pressing the mouse button.

- (void)setTitle:(NSString *)aString
Parameters
aString

The string to display.

Discussion

If the receiver displays a pop-up menu, this method changes the current item to be the item with the specified title, adding a new item by that name if one does not already exist. If the receiver displays a pull-down list, this method sets its title to the specified string.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

synchronizeTitleAndSelectedItem

Ensures that the item being displayed by the receiver agrees with the selected item.

- (void)synchronizeTitleAndSelectedItem
Discussion

If there’s no selected item, this method selects the first item in the item menu and sets the receiver’s item to match. For pull-down menus, this method makes sure that the first item is being displayed (the NSPopUpButtonCell object must be set to use the selected menu item, which happens by default).

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

titleOfSelectedItem

Returns the title of the item last selected by the user.

- (NSString *)titleOfSelectedItem
Return Value

The title of the selected menu item, or nil if no item is selected.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPopUpButton.h

Notifications

NSPopUpButtonWillPopUpNotification

Posted when an NSPopUpButton object receives a mouse-down event—that is, when the user is about to select an item from the menu.

The notification object is the selected NSPopUpButton object. This notification does not contain a userInfo dictionary.

Availability
Declared In
NSPopUpButton.h