iOS Reference Library Apple Developer
Search

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

Specifying the Data Source

Managing the Appearance of the Picker View

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

dataSource

The data source for the picker view.

@property(nonatomic, assign) id<UIPickerViewDataSource> dataSource

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.h

delegate

The delegate for the picker view.

@property(nonatomic, assign) id<UIPickerViewDelegate> delegate

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.h

numberOfComponents

Gets the number of components for the picker view. (read-only)

@property(nonatomic, readonly) NSInteger numberOfComponents

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.h

showsSelectionIndicator

A Boolean value that determines whether the selection indicator is displayed.

@property(nonatomic) BOOL showsSelectionIndicator

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.h

Instance Methods

numberOfRowsInComponent:

Returns the number of rows for a component.

- (NSInteger)numberOfRowsInComponent:(NSInteger)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.h

reloadAllComponents

Reloads all components of the picker view.

- (void)reloadAllComponents

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.
Declared In
UIPickerView.h

reloadComponent:

Reloads a particular component of the picker view.

- (void)reloadComponent:(NSInteger)component

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.
Declared In
UIPickerView.h

rowSizeForComponent:

Returns the size of a row for a component.

- (CGSize)rowSizeForComponent:(NSInteger)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.h

selectedRowInComponent:

Returns the index of the selected row in a given component.

- (NSInteger)selectedRowInComponent:(NSInteger)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.
Declared In
UIPickerView.h

selectRow:inComponent:animated:

Selects a row in a specified component of the picker view.

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

Parameters
row

A zero-indexed number identifying a row of component.

component

A zero-indexed number identifying a component of the picker view.

animated

YES to animate the selection by spinning the wheel (component) to the new value; if you specify NO, the new selection is shown immediately.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIPickerView.h

viewForRow:forComponent:

Returns the view used by the picker view for a given row and component.

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)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



Last updated: 2009-08-25

Did this document help you? Yes It's good, but... Not helpful...