- Inherits from:
- NSButton : NSControl : NSView : NSResponder : NSObject
- Conforms to:
- NSCoding
- (NSResponder)
- NSObject (NSObject)
Declared in:
- AppKit/NSPopUpButton.h
An NSPopUpButton object controls a pop-up list or a pull-down list, from which a user can select an item.
Interface Builder |
- 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 lists of the graphical user interface. You normally create an NSPopUpButton using Interface Builder.
When configured to display a pop-up list, an NSPopUpButton contains a number of options and usually displays the option that was last selected. A pop-up list is often used for selecting items from a small- to medium-sized set of options (like the zoom factor for a document window). It's a useful alternative to a matrix of radio buttons or an NSBrowser or NSTableView when screen space is at a premium; a zoom factor pop-up can easily fit next to a scroll bar at the bottom of a window, for example.
When configured to display a pull-down list, an NSPopUpButton is generally used for selecting commands in a very specific context. You can think of a pull-down list as a compact form of menu. A pull-down list's display isn't affected by the user's actions, and a pull-down list usually displays the first item in the list. When the commands only make sense in the context of a particular display, a pull-down list can be used in that display to keep the related actions nearby and to keep them out of the way when that display isn't visible.
Although the NSPopUpButton class defines an initialization method and methods that set up the list created by this class, you usually don't invoke these methods in your program. The typical way to create an NSPopUpButton is to use Interface Builder. You can define the NSPopUpButton's target and action, as well as the targets and actions of each item in the NSPopUpButton's list, programmatically or through Interface Builder. The NSPopUpButton methods you use most often are the methods that tell you which item is selected.
For example, suppose you want to create a pop-up list from which your user may select a language. You want your controller object to set an instance variable named language to an enum constant that corresponds to the value the user has chosen. You use Interface Builder to create the NSPopUpButton object, name it (languagePopUp in this example) add items to it, and configure it as a pop-up list. The actual code you write might look like this:
typedef enum _languageValue { English, French, German } languageValue; - (void)setLanguage:(id)sender{ NSString *title = [languagePopUp titleOfSelectedItem]; if ([title isEqualToString:@"English"]) language = English; else if ([title isEqualToString:@"French"]) language = French; else if ([title isEqualToString:@"German"]) language = German; }
- Initializing an NSPopUpButton
- - initWithFrame:pullsDown:
- Setting the type of list
- - setPullsDown:
- - pullsDown
- - setAutoenablesItems:
- - autoenablesItems
- Inserting and deleting Items
- - addItemWithTitle:
- - addItemsWithTitles:
- - insertItemWithTitle:atIndex:
- - removeAllItems
- - removeItemWithTitle:
- - removeItemAtIndex:
- Getting the user's selection
- - selectedItem
- - titleOfSelectedItem
- - indexOfSelectedItem
- Setting the current selection
- - selectItem:
- - selectItemAtIndex:
- - selectItemWithTitle:
- Getting menu items
- - numberOfItems
- - itemArray
- - itemAtIndex:
- - itemTitleAtIndex:
- - itemTitles
- - itemWithTitle:
- - lastItem
- Getting the indices of menu items
- - indexOfItem:
- - indexOfItemWithTag:
- - indexOfItemWithTitle:
- - indexOfItemWithRepresentedObject:
- - indexOfItemWithTarget:andAction:
- Setting the cell edge to pop out from in restricted situations
- - preferredEdge
- - setPreferredEdge:
- Setting the title
- - setTitle:
- Setting the state
- - synchronizeTitleAndSelectedItem
- (void)addItemWithTitle:(NSString
*)title
See Also: - insertItemWithTitle:atIndex:, - removeItemWithTitle:, - setTitle:
- (void)addItemsWithTitles:(NSArray
*)itemTitles
See Also: - insertItemWithTitle:atIndex:, - removeAllItems, - removeItemWithTitle:
- (BOOL)autoenablesItems
NO
to the NSPopUpButton.See Also: - setAutoenablesItems:
- (int)indexOfItem:(id
<NSMenuItem>)anObject
- (int)indexOfItemWithRepresentedObject:(id)anObject
- (int)indexOfItemWithTag:(int)tag
- (int)indexOfItemWithTarget:(id)target
andAction:(SEL)actionSelector
nil
,
the index of the first menu item in the pop-up list that has target target is
returned. If no menu item matching the above criteria is found,
-1 is returned. This method invokes the method of the same name
of its NSPopUpButtonCell.- (int)indexOfItemWithTitle:(NSString
*)title
- (int)indexOfSelectedItem
See Also: - selectedItem, - titleOfSelectedItem
- (id)initWithFrame:(NSRect)frameRect
pullsDown:(BOOL)flag
YES
,
the receiver is initialized to operate as a pull-down list; otherwise,
it operates as a pop-up list.See Also: - pullsDown, - setPullsDown:
- (void)insertItemWithTitle:(NSString
*)title
atIndex:(int)index
If an item with the name title already exists in the list, it's removed and the new one is added. This essentially moves title to a new position. If you want to move an item, it's better to invoke removeItemWithTitle: explicitly and then send this method.
See Also: - addItemWithTitle:, - addItemsWithTitles:, - indexOfItemWithTitle:, - removeItemWithTitle:
- (NSArray *)itemArray
See Also: - itemAtIndex:, - insertItemWithTitle:atIndex:, - removeItemAtIndex:
- (id <NSMenuItem>)itemAtIndex:(int)index
nil
.See Also: - itemWithTitle:, - lastItem
- (NSString *)itemTitleAtIndex:(int)index
See Also: - itemTitles
- (NSArray *)itemTitles
See Also: - itemTitleAtIndex: - itemWithTitle:, - numberOfItems
- (id <NSMenuItem>)itemWithTitle:(NSString
*)title
nil
.See Also: - addItemWithTitle:, - selectItemWithTitle:, - itemAtIndex:, - indexOfItemWithTitle:
- (id <NSMenuItem>)lastItem
See Also: - itemAtIndex:
- (NSMenu *)menu
- (int)numberOfItems
See Also: - lastItem
- (NSRectEdge)preferredEdge
See Also: - setPreferredEdge:
- (BOOL)pullsDown
YES
if
the receiver is configured as a pull-down list or NO
if
it's configured as a pop-up list.See Also: - setPullsDown:
- (void)removeAllItems
See Also: - numberOfItems, - removeItemAtIndex:, - removeItemWithTitle:
- (void)removeItemAtIndex:(int)index
See Also: - insertItemWithTitle:atIndex:, - removeAllItems, - removeItemWithTitle:
- (void)removeItemWithTitle:(NSString
*)title
See Also: - addItemWithTitle:, - removeAllItems, - removeItemAtIndex:
- (void)selectItem(id
<NSMenuItem>)anObject
nil
,
all items in the list are deselected (this is a technique for obtaining
a pop-up list with no items selected).- (void)selectItemAtIndex:(int)index
See Also: - indexOfSelectedItem
- (void)selectItemWithTitle:(NSString
*)title
See Also: - indexOfItemWithTitle:, - addItemWithTitle:, - setTitle:
- (id <NSMenuItem>)selectedItem
nil
. It is possible
for a pull-down list's selected item to be its first item.- (void)setAutoenablesItems:(BOOL)flag
NO
as
the value for flag. See Also: - autoenablesItems
- (void)setMenu:(NSMenu
*)menu
- (void)setPreferredEdge:(NSRectEdge)edge
See Also: - preferredEdge
- (void)setPullsDown:(BOOL)flag
YES
,
the receiver is configured as a pull-down list. If flag is NO
,
the receiver is configured as a pop-up list.See Also: - initWithFrame:pullsDown:, - pullsDown
- (int)setTitle:(NSString
*)aString
- (void)synchronizeTitleAndSelectedItem
See Also: - itemArray
- (NSString *)titleOfSelectedItem
See Also: - indexOfSelectedItem