Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/AppKit.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSPopUpButton.h |
Related sample code |
An NSPopUpButton
object controls a pop-up menu or a pull-down menu from which a user can select an item.
An NSMenu
Use Interface Builder to add a pop-up or pull-down menu to a window or panel.
selectedItem
Returns the currently selected item.
indexOfSelectedItem
Returns an integer identifying the currently selected item.
titleOfSelectedItem
Returns a string identifying the currently selected item.
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, adding, removing, or changing items on the menu is not reflected.
– addItemWithTitle:
– addItemsWithTitles:
– insertItemWithTitle:atIndex:
– removeAllItems
– removeItemWithTitle:
– removeItemAtIndex:
– menu
– setMenu:
– numberOfItems
– itemArray
– itemAtIndex:
– itemTitleAtIndex:
– itemTitles
– itemWithTitle:
– lastItem
– indexOfItem:
– indexOfItemWithTag:
– indexOfItemWithTitle:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTarget:andAction:
Adds multiple items to the end of the menu.
- (void)addItemsWithTitles:(NSArray *)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.
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.
NSPopUpButton.h
Adds an item with the specified title to the end of the menu.
- (void)addItemWithTitle:(NSString *)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.
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.
NSPopUpButton.h
Returns whether the receiver automatically enables and disables its items every time a user event occurs.
- (BOOL)autoenablesItems
YES
if the receiver automatically enables and disables items; otherwise, NO
. The default value is YES
.
For more information on enabling and disabling menu items, see the NSMenuValidation protocol specification.
NSPopUpButton.h
Returns the index of the specified menu item.
- (NSInteger)indexOfItem:(NSMenuItem *)anObject
The menu item whose index you want.
The index of the item or -1
if no such item was found.
This method invokes the method of the same name of its NSPopUpButtonCell
object.
NSPopUpButton.h
Returns the index of the menu item that holds the specified represented object.
- (NSInteger)indexOfItemWithRepresentedObject:(id)anObject
The represented object associated with a menu item.
The index of the menu item that owns the specified object, or -1
if no such menu item was found.
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.
NSPopUpButton.h
Returns the index of the menu item with the specified tag.
- (NSInteger)indexOfItemWithTag:(NSInteger)tag
The tag of the menu item you want.
The index of the item or -1
if no item with the specified tag was found.
This method invokes the method of the same name of its NSPopUpButtonCell
object.
NSPopUpButton.h
Returns the index of the menu item with the specified target and action.
- (NSInteger)indexOfItemWithTarget:(id)target andAction:(SEL)actionSelector
The target object associated with the menu item.
The action method associated with the menu item.
The index of the menu item, or -1
if no menu item contains the specified target and action.
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.
NSPopUpButton.h
Returns the index of the item with the specified title.
- (NSInteger)indexOfItemWithTitle:(NSString *)title
The title of the item you want.
The index of the item or -1
if no item with the specified title was found.
NSPopUpButton.h
Returns the index of the item last selected by the user.
- (NSInteger)indexOfSelectedItem
The index of the selected item, or -1
if no item is selected.
NSPopUpButton.h
Returns an NSPopUpButton
object initialized to the specified dimensions.
- (id)initWithFrame:(NSRect)frameRect pullsDown:(BOOL)flag
The frame rectangle for the button, specified in the parent view's coordinate system.
YES
if you want the receiver to display a pull-down menu; otherwise, NO
if you want it to display a pop-up menu.
An initialized NSPopUpButton
object, or nil
if the object could not be initialized.
NSPopUpButton.h
Inserts an item at the specified position in the menu.
- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index
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
The zero-based index at which to insert the item. Specifying 0 inserts the item at the top of the menu.
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.
NSPopUpButton.h
Returns the items in the menu.
- (NSArray *)itemArray
An array of id <NSMenuItem>
objects representing the items in the menu.
Usually you access the menu’s items and modify the menu using the methods of NSPopUpButton
rather than accessing the array of items directly.
NSPopUpButton.h
Returns the menu item at the specified index.
- (NSMenuItem *)itemAtIndex:(NSInteger)index
The index of the item you want.
The menu item, or nil
if no item exists at the specified index.
NSPopUpButton.h
Returns the title of the item at the specified index.
- (NSString *)itemTitleAtIndex:(NSInteger)index
The index of the item you want.
The title of the item, or an empty string if no item exists at the specified index.
NSPopUpButton.h
Returns the titles of all of the items in the menu.
- (NSArray *)itemTitles
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.
If the menu contains separator items, the array contains an empty string (@””) for each separator item.
NSPopUpButton.h
Returns the menu item with the specified title.
- (NSMenuItem *)itemWithTitle:(NSString *)title
The title of the menu item you want.
The menu item, or nil
if no item with the specified title exists in the menu.
NSPopUpButton.h
Returns the last item in the menu.
- (NSMenuItem *)lastItem
The last menu item.
NSPopUpButton.h
Returns the pop-up button’s associated menu.
- (NSMenu *)menu
The menu for the pop-up button.
NSPopUpButton.h
Returns the number of items in the menu.
- (NSInteger)numberOfItems
The number of items in the menu.
NSPopUpButton.h
Returns the index of the selected item.
- (id)objectValue
An object (typically an NSNumber
object) that responds to the intValue
message and contains the index of the selected item.
Returns the edge of the receiver next to which the pop-up menu is displayed under restrictive screen conditions.
- (NSRectEdge)preferredEdge
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.
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.
NSPopUpButton.h
Returns a Boolean value indicating the behavior of the control's menu.
- (BOOL)pullsDown
YES
if the menu behaves like a pull-down menu; otherwise, NO
if it behaves like a pop-up menu.
NSPopUpButton.h
Removes all items in the receiver’s item menu.
- (void)removeAllItems
After removing the items, this method uses the synchronizeTitleAndSelectedItem
method to refresh the menu.
NSPopUpButton.h
Removes the item at the specified index.
- (void)removeItemAtIndex:(NSInteger)index
The zero-based index indicating which item to remove. Specifying 0 removes the item at the top of the menu.
After removing the item, this method uses the synchronizeTitleAndSelectedItem
method to make sure the title displayed matches the currently selected item.
NSPopUpButton.h
Removes the item with the specified title from the menu.
- (void)removeItemWithTitle:(NSString *)title
The title of the item you want to remove. If no menu item exists with the specified title, this method triggers an assertion.
This method removes the first item it finds with the specified name. This method then uses synchronizeTitleAndSelectedItem
to refresh the menu.
NSPopUpButton.h
Returns the menu item last selected by the user.
- (NSMenuItem *)selectedItem
The menu item that is currently selected, or nil
if no item is selected.
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.
NSPopUpButton.h
Selects the specified menu item.
- (void)selectItem:(NSMenuItem *)item
The menu item to select, or nil
if you want to deselect all menu items.
NSPopUpButton.h
Selects the item in the menu at the specified index.
- (void)selectItemAtIndex:(NSInteger)index
The index of the item you want to select, or -1
you want to deselect all menu items.
NSPopUpButton.h
Selects the menu item with the specified tag.
- (BOOL)selectItemWithTag:(NSInteger)tag
The tag of the item you want to select.
YES
if the item was successfully selected; otherwise, NO
.
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
.
NSPopUpButton.h
Selects the item with the specified title.
- (void)selectItemWithTitle:(NSString *)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.
NSPopUpButton.h
Sets whether the receiver automatically enables and disables its items every time a user event occurs.
- (void)setAutoenablesItems:(BOOL)flag
YES
if you want the receiver to automatically enable and disable items; otherwise, NO
.
NSPopUpButton.h
This method has no effect.
- (void)setImage:(NSImage *)anImage
The image to display.
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).
Sets the pop-up button’s associated menu .
- (void)setMenu:(NSMenu *)menu
The menu to associate with the pop-up button.
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.
NSPopUpButton.h
Selects the item at a specific index using an object value.
- (void)setObjectValue:(id)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.
Sets the edge of the receiver next to which the pop-up menu should appear under restrictive screen conditions.
- (void)setPreferredEdge:(NSRectEdge)edge
The preferred edge. Possible values include NSMinXEdge
, NSMinYEdge
, NSMaxXEdge
, or NSMaxYEdge
.
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.
NSPopUpButton.h
Sets whether the receiver behaves as a pull-down or pop-up menu.
- (void)setPullsDown:(BOOL)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.
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.
NSPopUpButton.h
Sets the string displayed in the receiver when the user isn’t pressing the mouse button.
- (void)setTitle:(NSString *)aString
The string to display.
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.
NSPopUpButton.h
Ensures that the item being displayed by the receiver agrees with the selected item.
- (void)synchronizeTitleAndSelectedItem
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).
NSPopUpButton.h
Returns the title of the item last selected by the user.
- (NSString *)titleOfSelectedItem
The title of the selected menu item, or an empty string if no item is selected.
NSPopUpButton.h
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.
NSPopUpButton.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)