iOS Developer Library

Developer

UIKit Framework Reference UIPickerView Class Reference

Options
Deployment Target:

On This Page
Language:

UIPickerView

Inheritance


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.

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

For more information about appearance and behavior configuration, see Picker Views.

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

    Declaration

    Swift

    var numberOfComponents: Int { get }

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the number of rows for a component.

    Declaration

    Swift

    func numberOfRowsInComponent(_ component: Int) -> Int

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the size of a row for a component.

    Declaration

    Swift

    func rowSizeForComponent(_ component: Int) -> CGSize

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Reloads all components of the picker view.

    Declaration

    Swift

    func reloadAllComponents()

    Objective-C

    - (void)reloadAllComponents

    Discussion

    Calling this method causes the picker view to query the delegate for new data for all components.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Reloads a particular component of the picker view.

    Declaration

    Swift

    func reloadComponent(_ component: Int)

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func selectRow(_ row: Int, inComponent component: Int, animated animated: Bool)

    Objective-C

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

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func selectedRowInComponent(_ component: Int) -> Int

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func viewForRow(_ row: Int, forComponent component: Int) -> UIView?

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • delegate delegate Property

    The delegate for the picker view.

    Declaration

    Swift

    unowned(unsafe) var delegate: UIPickerViewDelegate?

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The data source for the picker view.

    Declaration

    Swift

    unowned(unsafe) var dataSource: UIPickerViewDataSource?

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    var showsSelectionIndicator: Bool

    Objective-C

    @property(nonatomic) BOOL showsSelectionIndicator

    Discussion

    If the value of the property is YEStrue, the picker view shows a clear overlay across the current row. The default value of this property is NOfalse.

    Special Considerations

    On iOS 7 and later you cannot customzie the picker view’s selection indicator. The selection indicator is always shown, so setting this property to NOfalse has no effect.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.