UIPickerViewDelegate Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 2.0 and later.
Declared in
UIPickerView.h
Related sample code

Overview

The delegate of a UIPickerView object must adopt this protocol and implement at least some of its methods to provide the picker view with the data it needs to construct itself.

The delegate implements the required methods of this protocol to return height, width, row title, and the view content for the rows in each component. It must also provide the content for each component’s row, either as a string or a view. Typically the delegate implements other optional methods to respond to new selections or deselections of component rows.

See UIPickerView Class Reference for a discussion of components, rows, row content, and row selection.

Tasks

Setting the Dimensions of the Picker View

Setting the Content of Component Rows

The methods in this group are marked @optional. However, to use a picker view, you must implement either the pickerView:titleForRow:forComponent: or the pickerView:viewForRow:forComponent:reusingView: method to provide the content of component rows.

Responding to Row Selection

Instance Methods

pickerView:attributedTitleForRow:forComponent:

Called by the picker view when it needs the styled title to use for a given row in a given component.

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
Parameters
pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

The attributed string to use as the title of the indicated component row.

Discussion

If you implement both this method and the pickerView:titleForRow:forComponent: method, the picker view prefers the use of this method. However, if your implementation of this method returns nil, the picker view falls back to using the string returned by the pickerView:titleForRow:forComponent: method.

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

pickerView:didSelectRow:inComponent:

Called by the picker view when the user selects a row in a component.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Parameters
pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Discussion

To determine what value the user selected, the delegate uses the row index to access the value at the corresponding position in the array used to construct the component.

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

pickerView:rowHeightForComponent:

Called by the picker view when it needs the row height to use for drawing row content.

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
Parameters
pickerView

The picker view requesting this information.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

A float value indicating the height of the row in points.

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

pickerView:titleForRow:forComponent:

Called by the picker view when it needs the title to use for a given row in a given component.

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
Parameters
pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

The string to use as the title of the indicated component row.

Discussion

If you implement both this method and the pickerView:attributedTitleForRow:forComponent: method, the picker view prefers the pickerView:attributedTitleForRow:forComponent: method. However, if that method returns nil, the picker view falls back to using the string returned by this method.

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

pickerView:viewForRow:forComponent:reusingView:

Called by the picker view when it needs the view to use for a given row in a given component.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
Parameters
pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

view

A view object that was previously used for this row, but is now hidden and cached by the picker view.

Return Value

A view object to use as the content of row. The object can be any subclass of UIView, such as UILabel, UIImageView, or even a custom view.

Discussion

If the previously used view (the view parameter) is adequate, return that. If you return a different view, the previously used view is released. The picker view centers the returned view in the rectangle for row.

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

pickerView:widthForComponent:

Called by the picker view when it needs the row width to use for drawing row content.

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
Parameters
pickerView

The picker view requesting this information.

component

A zero-indexed number identifying a component of the picker view. Components are numbered left-to-right.

Return Value

A float value indicating the width of the row in points.

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