Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/UIKit.framework |
Availability | Available in iOS 2.0 and later. |
Declared in | UIPickerView.h |
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.
delegate
property
dataSource
property
showsSelectionIndicator
property
For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.
The data source for the picker view.
@property(nonatomic, assign) id<UIPickerViewDataSource> dataSource
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.
UIPickerView.h
The delegate for the picker view.
@property(nonatomic, assign) id<UIPickerViewDelegate> delegate
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.
UIPickerView.h
Gets the number of components for the picker view. (read-only)
@property(nonatomic, readonly) NSInteger numberOfComponents
A UIPickerView
object fetches the value of this property from the data source and and caches it. The default value is zero.
UIPickerView.h
A Boolean value that determines whether the selection indicator is displayed.
@property(nonatomic) BOOL showsSelectionIndicator
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
.
UIPickerView.h
Returns the number of rows for a component.
- (NSInteger)numberOfRowsInComponent:(NSInteger)component
A zero-indexed number identifying a component.
The number of rows in the given component.
A picker view fetches the value of this property from the data source and and caches it. The default value is zero.
UIPickerView.h
Reloads all components of the picker view.
- (void)reloadAllComponents
Calling this method causes the picker view to query the delegate for new data for all components.
UIPickerView.h
Reloads a particular component of the picker view.
- (void)reloadComponent:(NSInteger)component
A zero-indexed number identifying a component of the picker view.
Calling this method causes the picker view to query the delegate for new data for the given component.
UIPickerView.h
Returns the size of a row for a component.
- (CGSize)rowSizeForComponent:(NSInteger)component
A zero-indexed number identifying a component.
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.
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).
UIPickerView.h
Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component
A zero-indexed number identifying a component of the picker view.
A zero-indexed number identifying the selected row, or -1
if no row is selected.
UIPickerView.h
Selects a row in a specified component of the picker view.
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
A zero-indexed number identifying a row of component.
A zero-indexed number identifying a component of the picker view.
YES
to animate the selection by spinning the wheel (component) to the new value; if you specify NO
, the new selection is shown immediately.
UIPickerView.h
Returns the view used by the picker view for a given row and component.
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
The zero-indexed number of a row of the picker view.
The zero-indexed number of a component of the picker view.
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:
.
UIPickerView.h
Last updated: 2009-08-25