1.4. Picking Values with the UIPickerView
Problem
You want to allow the users of your app to select from a list of values.
Solution
Use the UIPickerView
class.
Discussion
A picker view is a graphical element that allows you to display a series of values to your users and allow them to pick one. The Timer section of the Clock app on the iPhone is a great example of this (Figure 1-10).

Figure 1-10. A picker view on top of the screen
As you can see, this specific picker view has two separate and independent visual elements. One is on the left, and one is on the right. The element on the left is displaying hours (such as 0, 1, 2 hours, etc.) and the one on the right is displaying minutes (such as 10, 11, 12 mins, etc.). These two items are called components. Each component has rows. Any item in any of the components is in fact represented by a row, as we will soon see. For instance, in the left component, “0 hours” is a row, “1” is a row, etc.
Let’s go ahead and create a picker view on our view controller’s view. If you don’t know where your view controller’s source code is, please have a look at Recipe 1.2, where this subject is discussed.
First let’s go to the top of the .m (implementation) file of our view controller and define our picker view:
@interfaceViewController()@property(nonatomic,strong)UIPickerView*myPicker;@end@implementationViewController...
Now let’s create the picker ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access