NSComboBox 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 | NSComboBox.h |
Overview
An NSComboBox is a kind of NSControl that allows you to either enter text directly (as you would with an NSTextField) or click the attached arrow at the right of the combo box and select from a displayed (“pop-up”) list of items.
Normally an instance of NSComboBox looks like this:

When you click the downward-pointing arrow at the right side of the text field, the pop-up list appears, like this:

The NSComboBox class uses NSComboBoxCell to implement its user interface.
Also see the NSComboBoxDataSource informal protocol, which declares the methods that an NSComboBox uses to access the contents of its data source object.
Tasks
Setting Display Attributes
-
– hasVerticalScroller -
– intercellSpacing -
– isButtonBordered -
– itemHeight -
– numberOfVisibleItems -
– setButtonBordered: -
– setHasVerticalScroller: -
– setIntercellSpacing: -
– setItemHeight: -
– setNumberOfVisibleItems:
Setting a Data Source
Working with an Internal List
-
– addItemsWithObjectValues: -
– addItemWithObjectValue: -
– insertItemWithObjectValue:atIndex: -
– objectValues -
– removeAllItems -
– removeItemAtIndex: -
– removeItemWithObjectValue: -
– numberOfItems
Manipulating the Displayed List
-
– indexOfItemWithObjectValue: -
– itemObjectValueAtIndex: -
– noteNumberOfItemsChanged -
– reloadData -
– scrollItemAtIndexToTop: -
– scrollItemAtIndexToVisible:
Manipulating the Selection
-
– deselectItemAtIndex: -
– indexOfSelectedItem -
– objectValueOfSelectedItem -
– selectItemAtIndex: -
– selectItemWithObjectValue:
Completing the Text Field
New Methods
Instance Methods
addItemsWithObjectValues:
Adds multiple objects to the end of the receiver’s internal item list.
Parameters
- objects
An array of the objects to add to the internal item list.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.haddItemWithObjectValue:
Adds an object to the end of the receiver’s internal item list.
Parameters
- anObject
The object to add to the internal item list.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hcompletes
Returns a Boolean value indicating whether the receiver tries to complete what the user types in the text field.
Return Value
YES if the receiver tries to complete what the user types in the text field; otherwise NO.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hdataSource
Returns the object that provides the data displayed in the receiver’s pop-up list.
Return Value
The data source for the combo box's pop-up list.
Discussion
This method logs a warning if usesDataSource returns NO. See the class description and the NSComboBoxDataSource informal protocol specification for more information on combo box data source objects.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hdelegate
Returns the receiver’s delegate.
Return Value
The receiver’s delegate.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSComboBox.hdeselectItemAtIndex:
Deselects the pop-up list item at the specified index if it’s selected.
Parameters
- index
The index of the item to deselect.
Discussion
If the selection does in fact change, this method posts an NSComboBoxSelectionDidChangeNotification to the default notification center.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hhasVerticalScroller
Returns a Boolean value indicating whether the receiver will display a vertical scroller.
Return Value
YES if the receiver will display a vertical scroller; otherwise NO.
Discussion
Note that the scroller will be displayed even if the pop-up list contains fewer items than will fit in the area specified for display.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hindexOfItemWithObjectValue:
Searches the receiver’s internal item list for the specified object and returns the lowest matching index.
Parameters
- anObject
The object for which to return the index.
Return Value
The lowest index in the internal item list whose corresponding value is equal to that of the specified object. Objects are considered equal if they have the same id or if isEqual: returns YES.
If none of the objects in the receiver’s internal item list are equal to anObject, indexOfItemWithObjectValue: returns NSNotFound.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hindexOfSelectedItem
Returns the index of the last item selected from the pop-up list.
Return Value
The index of the last item selected from the receiver's pop-up list or -1 if no item is selected.
Discussion
Note that nothing is initially selected in a newly initialized combo box.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hinsertItemWithObjectValue:atIndex:
Inserts an object at the specified location in the receiver’s internal item list.
Parameters
- anObject
The object to add to the internal item list.
- index
The index in the list at which to add the new object. The previous item at index—along with all following items—is shifted down one slot to make room
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hintercellSpacing
Returns the horizontal and vertical spacing between cells in the receiver’s pop-up list.
Return Value
The space between cells in the pop-up list. The default spacing is (3.0, 2.0).
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hisButtonBordered
Returns whether the combo box button is set to display a border.
Return Value
YES if the button has a border; otherwise NO.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSComboBox.hitemHeight
Returns the height of each item in the receiver’s pop-up list.
Return Value
The height of items in the pop-up list. The default item height is 16.0.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hitemObjectValueAtIndex:
Returns the object located at the given index within the receiver’s internal item list.
Parameters
- index
The index of the object to retrieve. If index is beyond the end of the list, an
NSRangeExceptionis raised.
Return Value
The object located at the specified index in the internal item list.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hnoteNumberOfItemsChanged
Informs the receiver that the number of items in its data source has changed.
Discussion
This method allows the receiver to update the scrollers in its displayed pop-up list without actually reloading data into the receiver. It is particularly useful for a data source that continually receives data in the background over a period of time, in which case the NSComboBox can remain responsive to the user while the data is received.
See the NSComboBoxDataSource informal protocol specification for information on the messages an NSComboBox sends to its data source.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hnumberOfItems
Returns the total number of items in the pop-up list.
Return Value
The number of items in the list.
Availability
- Available in OS X v10.0 and later.
See Also
-
– numberOfVisibleItems -
– numberOfItemsInComboBox:(NSComboBoxDataSourceprotocol)
Declared In
NSComboBox.hnumberOfVisibleItems
Returns the maximum number of items visible in the pop-up list.
Return Value
The maximum number of items visible at any one time in the pop-up list.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hobjectValueOfSelectedItem
Returns the object corresponding to the last item selected from the pop-up list.
Return Value
The object in the receiver's internal item list corresponding to the last item selected from the pop-up list, or nil if no item is selected.
Discussion
Note that nothing is initially selected in a newly initialized combo box. This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
-
– indexOfSelectedItem -
– comboBox:objectValueForItemAtIndex:(NSComboBoxDataSource protocol)
Declared In
NSComboBox.hobjectValues
Returns as an array the receiver’s internal item list.
Return Value
The array containing the objects in the receiver's internal item list.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hreloadData
Marks the receiver as needing redisplay, so that it will reload the data for visible pop-up items and draw the new values.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hremoveAllItems
Removes all items from the receiver’s internal item list.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hremoveItemAtIndex:
Removes the object at the specified location from the receiver’s internal item list.
Parameters
- index
The index of the object to remove. All items beyond index are moved up one slot to fill the gap.
Discussion
The removed object receives a release message. This method raises an NSRangeException if index is beyond the end of the list and logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hremoveItemWithObjectValue:
Removes all occurrences of the given object from the receiver’s internal item list.
Parameters
- anObject
The object to remove from the internal item list. Objects are considered equal if they have the same id or if
isEqual:returnsYES.
Discussion
This method logs a warning if usesDataSource returns YES.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hscrollItemAtIndexToTop:
Scrolls the receiver’s pop-up list vertically so that the item at the specified index is as close to the top as possible.
Parameters
- index
The index of the item to scroll to the top.
Discussion
The pop-up list need not be displayed at the time this method is invoked.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hscrollItemAtIndexToVisible:
Scrolls the receiver’s pop-up list vertically so that the item at the specified index is visible.
Parameters
- index
The index of the item to make visible.
Discussion
The pop-up list need not be displayed at the time this method is invoked.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hselectItemAtIndex:
Selects the pop-up list row at the given index.
Parameters
- index
The index of the item to select in the pop-up list.
Discussion
Posts an NSComboBoxSelectionDidChangeNotification to the default notification center if the selection does in fact change. Note that this method does not alter the contents of the combo box’s text field—see Setting the Combo Boxâs Value for more information.
Availability
- Available in OS X v10.0 and later.
See Also
-
– setObjectValue:(NSControl)
Declared In
NSComboBox.hselectItemWithObjectValue:
Selects the first pop-up list item that corresponds to the given object.
Parameters
- anObject
The object to select in the pop-up list. Objects are considered equal if they have the same id or if
isEqual:returnsYES.
Discussion
This method logs a warning if usesDataSource returns YES. Posts an NSComboBoxSelectionDidChangeNotification to the default notification center if the selection does in fact change. Note that this method doesn’t alter the contents of the combo box’s text field—see Setting the Combo Boxâs Value for more information.
Availability
- Available in OS X v10.0 and later.
See Also
-
– setObjectValue:(NSControl)
Declared In
NSComboBox.hsetButtonBordered:
Determines whether the button in the combo box is displayed with a border.
Parameters
- flag
YESto display a border;NOto display the button without a border. For example, it is often useful when using a combo box in anNSTableViewto display the button without the border.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSComboBox.hsetCompletes:
Sets whether the receiver tries to complete what the user types in the text field.
Parameters
- completes
YESto indicate that the receiver should try to complete test entered by the user. If completes isYES, every time the user adds characters to the end of the text field, the combo box calls theNSComboBoxCellmethodcompletedString:.
Discussion
If completedString: returns a string that’s longer than the existing string, the combo box replaces the existing string with the returned string and selects the additional characters. If the user is deleting characters or adds characters somewhere besides the end of the string, the combo box does not try to complete it.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hsetDataSource:
Sets the receiver’s data source to aSource.
Parameters
- aSource
The new data source for the receiver. The data source should implement the appropriate methods of the
NSComboBoxDataSourceinformal protocol.This method logs a warning if aSource doesn’t respond to either
numberOfItemsInComboBox:orcomboBox:objectValueForItemAtIndex:.
Discussion
This method doesn’t automatically set usesDataSource to NO and in fact logs a warning if usesDataSource returns NO.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hsetDelegate:
Sets the receiver’s delegate.
Parameters
- anObject
The delegate for the receiver. The delegate must conform to the
NSComboBoxDelegate Protocolprotocol.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSComboBox.hsetHasVerticalScroller:
Determines whether the receiver displays a vertical scroller.
Parameters
- flag
YESto display a vertical scroller;NOotherwise. By default, flag isYES.
Discussion
If flag is NO and the combo box has more list items (either in its internal item list or from its data source) than are allowed by numberOfVisibleItems, only a subset are displayed. The NSComboBox class' scroll... methods can be used to position this subset within the pop-up list.
Note that if flag is YES, a scroller will be displayed even if the combo box has fewer list items than are allowed by numberOfVisibleItems.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hsetIntercellSpacing:
Sets the spacing between pop-up list items.
Parameters
- aSize
The new width and height between pop-up list items. The default intercell spacing is (3.0, 2.0).
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hsetItemHeight:
Sets the height for items.
Parameters
- itemHeight
The new height for items in the pop-up list.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hsetNumberOfVisibleItems:
Sets the maximum number of items that are visible in the receiver’s pop-up list.
Parameters
- visibleItems
The maximum number of items that are visible at one time in the pop-up list.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hsetUsesDataSource:
Sets whether the receiver uses an external data source to populate the receiver’s pop-up list.
Parameters
- flag
YESif the receiver uses an external data source (specified bysetDataSource:); otherwiseNO.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.husesDataSource
Returns a Boolean value indicating whether the receiver uses an external data source to populate its pop-up list.
Return Value
YES if the receiver uses an external data source to populate the receiver’s pop-up list, NO if it uses an internal item list.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSComboBox.hNotifications
NSComboBoxSelectionDidChangeNotification
NSComboBox changes. The notification object is the NSComboBox whose selection changed. This notification does not contain a userInfo dictionary.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hNSComboBoxSelectionIsChangingNotification
NSComboBox is changing. The notification object is the NSComboBox whose selection is changing. This notification does not contain a userInfo dictionary.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hNSComboBoxWillDismissNotification
NSComboBox is about to be dismissed.The notification object is the NSComboBox whose pop-up list will be dismissed. This notification does not contain a userInfo dictionary.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.hNSComboBoxWillPopUpNotification
NSComboBox is going to be displayed. The notification object is the NSComboBox whose pop-up window will be displayed. This notification does not contain a userInfo dictionary.
Availability
- Available in OS X v10.0 and later.
Declared In
NSComboBox.h© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-04-15)