Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/AppKit.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSPopUpButtonCell.h |
Related sample code |
The NSPopUpButtonCell
class defines the visual appearance of pop-up buttons that display pop-up or pull-down menus. Pop-up menus present the user with a set of choices, much the way radio buttons do, but using much less space. Pull-down menus also provide a set of choices but present the information in a slightly different way, usually to provide a set of commands from which the user can choose.
The NSPopUpButtonCell
class implements the user interface for the NSPopUpButton
class.
Note that while a menu is tracking, adding, removing, or changing items on the menu is not reflected.
– setMenu:
– menu
– setPullsDown:
– pullsDown
– setAutoenablesItems:
– autoenablesItems
– setPreferredEdge:
– preferredEdge
– setUsesItemFromMenu:
– usesItemFromMenu
– setAltersStateOfSelectedItem:
– altersStateOfSelectedItem
– setArrowPosition:
– arrowPosition
– addItemWithTitle:
– addItemsWithTitles:
– insertItemWithTitle:atIndex:
– removeItemWithTitle:
– removeItemAtIndex:
– removeAllItems
– itemArray
– numberOfItems
– indexOfItem:
– indexOfItemWithTitle:
– indexOfItemWithTag:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTarget:andAction:
– itemAtIndex:
– itemWithTitle:
– lastItem
– setObjectValue:
– objectValue
– selectItem:
– selectItemAtIndex:
– selectItemWithTag:
– selectItemWithTitle:
– setTitle:
– selectedItem
– indexOfSelectedItem
– synchronizeTitleAndSelectedItem
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.
The new menu items use the pop-up button’s default action and target, but you can change these using the setAction:
and setTarget:
methods of the corresponding NSMenuItem
object.
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.
– addItemWithTitle:
– setAction:
(NSMenuItem
)– setTarget:
(NSMenuItem
)NSPopUpButtonCell.h
Adds an item with the specified title to the end of the menu.
- (void)addItemWithTitle:(NSString *)title
The title of the new menu 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 menu item uses the pop-up button’s default action and target, but you can change these using the setAction:
and setTarget:
methods of the corresponding NSMenuItem
object.
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 button's menu instead.
– addItemsWithTitles:
– setAction:
(NSMenuItem
)– setTarget:
(NSMenuItem
)NSPopUpButtonCell.h
Returns a Boolean value indicating whether the receiver links the state of the selected menu item to the current selection.
- (BOOL)altersStateOfSelectedItem
YES
if the selected menu item has its state set to NSOnState
automatically; otherwise, NO
if the state of menu items is independent of the current selection.
This option is usually used only by pop-up menus. You typically do not alter the state of menu items in a pull-down menu.
NSPopUpButtonCell.h
Returns the position of the arrow displayed on the receiver.
- (NSPopUpArrowPosition)arrowPosition
The arrow position.
NSPopUpNoArrow
means no arrow is displayed. NSPopUpArrowAtCenter
means the arrow is vertically centered, pointing to the right, vertically centered. NSPopUpArrowAtBottom
means the arrow is at the bottom, pointing downward.
NSPopUpButtonCell.h
Sets up the receiver to display a menu.
- (void)attachPopUpWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
The cell's rectangle, specified in points in the coordinate system of the view in the controlView parameter. The menu is attached to this rectangle.
The view in which to display the pop-up button's menu.
This call sets up the popup button cell to display a menu, which occurs in performClickWithFrame:inView:
. This method sets the cell's control view and then highlights and redraws the cell. It does not show the menu.
This method also posts an NSPopUpButtonCellWillPopUpNotification
. (The NSPopUpButton
object sends a corresponding NSPopUpButtonWillPopUpNotification
.)
You normally do not call this method explicitly. It is called by the Application Kit automatically when the menu for the pop-up button is to be displayed.
NSPopUpButtonCell.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 .
NSPopUpButtonCell.h
Dismisses the pop-up button’s menu by ordering its window out.
- (void)dismissPopUp
If the pop-up button was not displaying its menu, this method does nothing.
You normally do not call this method explicitly. It is called by the Application Kit automatically to dismiss the menu for the pop-up button.
– attachPopUpWithFrame:inView:
– orderOut:
(NSWindow
)NSPopUpButtonCell.h
Returns the index of the specified menu item.
- (NSInteger)indexOfItem:(NSMenuItem *)item
The menu item whose index you want.
The index of the item or -1
if no such item was found.
– indexOfItemWithRepresentedObject:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
– indexOfSelectedItem
NSPopUpButtonCell.h
Returns the index of the menu item that holds the specified represented object.
- (NSInteger)indexOfItemWithRepresentedObject:(id)obj
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.
– indexOfItem:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
– indexOfSelectedItem
– representedObject
(NSMenuItem)– setRepresentedObject:
(NSMenuItem)NSPopUpButtonCell.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.
Tags are values your application assigns to an object to identify it. You can assign tags to menu items using the setTag:
method of NSMenuItem
.
– indexOfItem:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
– indexOfSelectedItem
– setTag:
(NSMenuItem
)NSPopUpButtonCell.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.
The NSPopUpButtonCell
class assigns a default action and target to each menu item, but you can change these values using the setAction:
and setTarget:
methods of NSMenuItem
.
– indexOfItem:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
– indexOfSelectedItem
– setAction:
(NSMenuItem
)– setTarget:
(NSMenuItem
)NSPopUpButtonCell.h
Returns the index of the item with the specified title.
- (NSInteger)indexOfItemWithTitle:(NSString *)title
The title of the item you want. You must not pass nil
for this parameter.
The index of the item or -1
if no item with the specified title was found.
– indexOfItem:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
– indexOfSelectedItem
NSPopUpButtonCell.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.
– indexOfItem:
– indexOfItemWithRepresentedObject:
– indexOfItemWithTag:
– indexOfItemWithTarget:andAction:
– indexOfItemWithTitle:
NSPopUpButtonCell.h
Returns an NSPopUpButtonCell
object initialized with the specified title.
- (id)initTextCell:(NSString *)stringValue pullsDown:(BOOL)pullDown
The title of the first menu. You may specify an empty string if you do not want to add an initial menu item.
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 NSPopUpButtonCell
object, or nil
if the object could not be initialized.
This menu item is assigned the default pop-up button action that displays the menu. To set the action and target, use the setAction:
and setTarget:
methods of the item’s corresponding NSMenuItem
object.
This method is the designated initializer of the class.
– setAction:
(NSMenuItem
)– setTarget:
(NSMenuItem
)NSPopUpButtonCell.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.
The value in index must represent a valid position in the array. The menu item at index and all those that follow it are shifted down one slot to make room for the new menu item.
This method assigns the pop-up button’s default action and target to the new menu item. Use the menu item’s setAction:
and setTarget:
methods to assign a new action and target.
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 button's menu instead.
insertObject:atIndex:
(NSMutableArray
)– setAction:
(NSMenuItem
)– setTarget:
(NSMenuItem
)NSPopUpButtonCell.h
Returns the items in the menu.
- (NSArray *)itemArray
An array of NSMenuItem
objects representing the items in the menu.
– itemArray
(NSMenu)NSPopUpButtonCell.h
Returns the menu item at the specified index.
- (NSMenuItem *)itemAtIndex:(NSInteger)index
The index of the item you want. The specified index must refer to an existing menu item.
The menu item, or nil
if no item exists at the specified index.
– itemTitleAtIndex:
– itemAtIndex:
(NSMenu
)NSPopUpButtonCell.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.
NSPopUpButtonCell.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.
NSPopUpButtonCell.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.
NSPopUpButtonCell.h
Returns the last item in the menu.
- (NSMenuItem *)lastItem
The last menu item.
NSPopUpButtonCell.h
Returns the pop-up button’s associated menu.
- (NSMenu *)menu
The menu for the pop-up button.
NSPopUpButtonCell.h
Returns the number of items in the menu.
- (NSInteger)numberOfItems
The number of items in the menu.
count
(NSArray)NSPopUpButtonCell.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.
NSPopUpButtonCell.h
Displays the receiver’s menu and track mouse events in it.
- (void)performClickWithFrame:(NSRect)frame inView:(NSView *)controlView
The cell's rectangle, specified in points in the coordinate system of the view in the controlView parameter.
The view in which to display the pop-up button's menu.
You normally do not call this method explicitly. It is called by the Application Kit automatically to handle events in the pop-up button.
NSPopUpButtonCell.h
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
. If no preferred edge was explicitly set, the default value is the bottom edge, which is NSMaxYEdge
for flipped views or NSMinYEdge
for unflipped views.
NSPopUpButtonCell.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.
NSPopUpButtonCell.h
Removes all items in the receiver’s item menu.
- (void)removeAllItems
NSPopUpButtonCell.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. The index must be valid and non-negative.
NSPopUpButtonCell.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.
NSPopUpButtonCell.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.
NSPopUpButtonCell.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.
By default, selecting or deselecting a menu item from a pop-up menu changes its state. Selecting a menu item from a pull-down menu does not automatically alter the state of the item. Use the setAltersStateOfSelectedItem:
method, passing it a value of NO
, to disassociate the current selection from the state of menu items.
– selectedItem
– selectItemAtIndex:
– selectItemWithTitle:
– setAltersStateOfSelectedItem:
– setState:
(NSMenuItem
)NSPopUpButtonCell.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.
By default, selecting or deselecting a menu item from a pop-up menu changes its state. Selecting a menu item from a pull-down menu does not automatically alter the state of the item. Use the setAltersStateOfSelectedItem:
method, passing it a value of NO
, to disassociate the current selection from the state of menu items.
Subclassers can override this method to catch all select calls.
– selectedItem
– selectItem:
– selectItemWithTitle:
– setAltersStateOfSelectedItem:
– setState:
(NSMenuItem
)NSPopUpButtonCell.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
.
NSPopUpButtonCell.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.
By default, selecting or deselecting a menu item changes its state. Use the setAltersStateOfSelectedItem:
method, passing it a value of NO
, to disassociate the current selection from the state of menu items.
– selectedItem
– selectItem:
– selectItemAtIndex:
– setAltersStateOfSelectedItem:
– setState:
(NSMenuItem
)NSPopUpButtonCell.h
Sets whether the receiver links the state of the menu items to the current selection.
- (void)setAltersStateOfSelectedItem:(BOOL)flag
YES
if the selected menu item has its state set to NSOnState
automatically; otherwise, NO
if the state of menu items is independent of the current selection.
You use this method to control whether the selected menu item is linked to the state of that item. When you specify NO
for the flag parameter, this method sets the state of the currently selected item to NSOffState
.
– altersStateOfSelectedItem
– selectedItem
– selectItem:
– selectItemAtIndex:
– setState:
(NSMenuItem
)NSPopUpButtonCell.h
Sets the position of the arrow displayed on the receiver.
- (void)setArrowPosition:(NSPopUpArrowPosition)position
The position of the arrow.
If you specify NSPopUpNoArrow
, the receiver displays no arrow. NSPopUpArrowAtCenter
displays the arrow centered horizontally within the cell. NSPopUpArrowAtBottom
displays the arrow at the edge of the cell. This method works with setPreferredEdge:
to determine the exact location and orientation of the arrow. For more information, see setPreferredEdge:
.
This method is ignored unless the receiver is a pull-down list with a beveled border.
NSPopUpButtonCell.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
.
NSPopUpButtonCell.h
This method has no effect.
- (void)setImage:(NSImage *)anImage
The image to display.
The image displayed in a pop up button 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.
NSPopUpButtonCell.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
message and return an appropriate index value.
NSPopUpButtonCell.h
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
.
At display time, if attaching the menu to the preferred edge would cause part of the menu to be obscured, the pop-up button may use a different edge. If no preferred edge is set, the pop-up button uses the bottom edge by default.
This method works with setArrowPosition:
to determine the exact location of the arrow:
If the arrow position is NSPopUpArrowAtCenter
, the arrow stays in the center of the button and this method determines which edge the arrow points to. NSMinXEdge
points to the left, NSMaxYEdge
points to the top, NSMaxXEdge
points to the right, and NSMinYEdge
points to the bottom.
If the arrow position is NSPopUpArrowAtBottom
, this method determines which edge the arrow is at. NSMinXEdge
places the arrow at the center of the left side, pointing to the left. NSMinYEdge
places the arrow at bottom right corner, pointing up. NSMaxXEdge
places the arrow at the center of the right side, pointing to the right. NSMaxYEdge
places the arrow at the bottom right corner, pointing down.
NSPopUpButtonCell.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 NSOffState
before changing the menu type.
NSPopUpButtonCell.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.
For pull-down menus that get their titles from a menu item, this method simply sets the pop-up button cell’s menu item to the first item in the menu. For pop-up menus, if a menu item whose title matches aString exists, this method makes that menu item the current selection; otherwise, it creates a new menu item with the title aString, adds it to the pop-up menu, and selects it.
NSPopUpButtonCell.h
Sets whether the pop-up button uses an item from the menu for its own title.
- (void)setUsesItemFromMenu:(BOOL)flag
YES
if the button should use the first menu item as its own title; otherwise, NO
. YES
is the default value.
For pop-up menus, the pop-up button uses the title of the currently selected menu item; if no menu item is selected, the pop-up button displays no item and is drawn empty. You can set the title or image of the pop-up button to something permanent by first calling this method (with a parameter of NO
) and then calling setMenuItem:
, as shown in the following example:
- (void)awakeFromNib { |
NSString *buttonImagePath = [[NSBundle mainBundle] pathForResource:@"plane" ofType:@"png"]; |
NSImage *buttonImage = [[NSImage alloc] initWithContentsOfFile:buttonImagePath]; |
NSMenuItem *imageItem = [[NSMenuItem alloc] init]; |
[imageItem setImage:buttonImage]; |
[imageItem setTitle:@"City"]; |
[[myPopUpButton cell] setUsesItemFromMenu:NO]; |
[[myPopUpButton cell] setMenuItem:imageItem]; |
[buttonImage release]; |
[imageItem release]; |
} |
NSPopUpButtonCell.h
Synchronizes the the pop-up button’s displayed item with the currently selected menu item.
- (void)synchronizeTitleAndSelectedItem
If no item is currently selected, this method synchronizes the pop-up buttons displayed item with the first menu item. If the pop-up button cell does not get its displayed item from a menu item, this method does nothing.
For pull-down menus, this method sets the displayed item to the title first menu item.
If the pop-up button’s menu does not contain any menu items, this method sets the pop-up button’s displayed item to nil
, resulting in nothing being displayed in the control.
NSPopUpButtonCell.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.
NSPopUpButtonCell.h
Returns a Boolean value indicating whether the pop-up button uses an item from the menu for its own title.
- (BOOL)usesItemFromMenu
YES
if the button uses the first menu item as its own title; otherwise, NO
. YES
is the default value.
If this option is set, pull-down menus use the title of the first menu item, while pop-up menus use the title of the currently selected menu.
NSPopUpButtonCell.h
These constants are defined for use with the arrowPosition
and setArrowPosition:
methods.
typedef enum { NSPopUpNoArrow = 0, NSPopUpArrowAtCenter = 1, NSPopUpArrowAtBottom = 2 } NSPopUpArrowPosition;
NSPopUpNoArrow
Does not display any arrow in the receiver.
Available in Mac OS X v10.0 and later.
Declared in NSPopUpButtonCell.h
.
NSPopUpArrowAtCenter
Arrow is centered vertically, pointing toward the preferredEdge
.
Available in Mac OS X v10.0 and later.
Declared in NSPopUpButtonCell.h
.
NSPopUpArrowAtBottom
Arrow is drawn at the edge of the button, pointing toward the preferredEdge
.
Available in Mac OS X v10.0 and later.
Declared in NSPopUpButtonCell.h
.
NSPopUpButtonCell.h
This notification is posted just before an pop-up menu is attached to its window frame. You can use this notification to lazily construct your part’s menus, thus preventing unnecessary calculations until they are needed. The notification object can be either a pop-up button or its enclosed pop-up button cell. This notification does not contain a userInfo dictionary.
NSPopUpButtonCell.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)