iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UIPickerViewDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

UIPickerViewDelegate

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

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.
  • Called by the picker view when it needs the row height to use for drawing row content.

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

    Objective-C

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

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.