UIPickerView Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UIPickerView.h |
Overview
The UIPickerView class implements objects, called picker views, that use a spinning-wheel or slot-machine metaphor to show one or more sets of values. Users select values by rotating the wheels so that the desired row of values aligns with a selection indicator.
The UIDatePicker class uses a custom subclass of UIPickerView to display dates and times. To see an example, tap the add (“+”) button in the the Alarm pane of the Clock application.
The user interface provided by a picker view consists of components and rows. A component is a wheel, which has a series of items (rows) at indexed locations on the wheel. Each component also has an indexed location (left to right) in a picker view. Each row on a component has content, which is either a string or a view object such as a label (UILabel) or an image (UIImageView).
A UIPickerView object requires the cooperation of a delegate for constructing its components and a data source for providing the numbers of components and rows. The delegate must adopt the UIPickerViewDelegate protocol and implement the required methods to return the drawing rectangle for rows in each component. It also provides the content for each component’s row, either as a string or a view, and it typically responds to new selections or deselections. The data source must adopt the UIPickerViewDataSource protocol and implement the required methods to return the number of components and the number of rows in each component.
You can dynamically change the rows of a component by calling the reloadComponent: method, or dynamically change the rows of all components by calling the reloadAllComponents method. When you call either of these methods, the picker view asks the delegate for new component and row data, and asks the data source for new component and row counts. Reload a picker view when a selected value in one component should change the set of values in another component. For example, changing a row value from February to March in one component should change a related component representing the days of the month.
Tasks
Getting the Dimensions of the View Picker
Reloading the View Picker
Selecting Rows in the View Picker
Returning the View for a Row and Component
Specifying the Delegate
-
delegateproperty
Specifying the Data Source
-
dataSourceproperty
Managing the Appearance of the Picker View
-
showsSelectionIndicatorproperty
Properties
dataSource
The data source for the picker view.
Discussion
The data source must adopt the UIPickerViewDataSource protocol and implement the required methods to return the number of components and the number of rows in each component.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hdelegate
The delegate for the picker view.
Discussion
The delegate must adopt the UIPickerViewDelegate protocol and implement the required methods to return the drawing rectangle for rows in each component. It also provides the content for each component’s row, either as a string or a view, and it typically responds to new selections or deselections.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hnumberOfComponents
Gets the number of components for the picker view. (read-only)
Discussion
A UIPickerView object fetches the value of this property from the data source and and caches it. The default value is zero.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hshowsSelectionIndicator
A Boolean value that determines whether the selection indicator is displayed.
Discussion
If the value of the property is YES, the picker view shows a clear overlay across the current row. The default value of this property is NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hInstance Methods
numberOfRowsInComponent:
Returns the number of rows for a component.
Parameters
- component
A zero-indexed number identifying a component.
Return Value
The number of rows in the given component.
Discussion
A picker view fetches the value of this property from the data source and and caches it. The default value is zero.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hreloadAllComponents
Reloads all components of the picker view.
Discussion
Calling this method causes the picker view to query the delegate for new data for all components.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIPickerView.hreloadComponent:
Reloads a particular component of the picker view.
Parameters
- component
A zero-indexed number identifying a component of the picker view.
Discussion
Calling this method causes the picker view to query the delegate for new data for the given component.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIPickerView.hrowSizeForComponent:
Returns the size of a row for a component.
Parameters
- component
A zero-indexed number identifying a component.
Return Value
The size of rows in the given component. This is generally the size required to display the largest string or view used as a row in the component.
Discussion
A picker view fetches the value of this property by calling the pickerView:widthForComponent: and pickerView:rowHeightForComponent: delegate methods, and caches it. The default value is (0, 0).
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.hselectedRowInComponent:
Returns the index of the selected row in a given component.
Parameters
- component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is selected.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIPickerView.hselectRow:inComponent:animated:
Selects a row in a specified component of the picker view.
Parameters
- row
A zero-indexed number identifying a row of component.
- component
A zero-indexed number identifying a component of the picker view.
- animated
YESto animate the selection by spinning the wheel (component) to the new value; if you specifyNO, the new selection is shown immediately.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIPickerView.hviewForRow:forComponent:
Returns the view used by the picker view for a given row and component.
Parameters
- row
The zero-indexed number of a row of the picker view.
- component
The zero-indexed number of a component of the picker view.
Return Value
The view provided by the delegate in the pickerView:viewForRow:forComponent:reusingView: method. Returns nil if the specified row of the component is not visible or if the delegate does not implement pickerView:viewForRow:forComponent:reusingView:.
Availability
- Available in iOS 2.0 and later.
Declared In
UIPickerView.h© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-08-25)