Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

Picking my favourite car

1.83/5 (6 votes)
18 Feb 2016CPOL1 min read 11.9K   35  
Using the UIPickerView in iOS

Introduction

I want to learn the shiny UIPickerView from UIKit for some selecting tasks. By searching the web I stumbled at nice video on Youtube from Bruce Donnerson which covers these basics and so decided to practice and enhance it and share my knowledge.

The final result looks like this:

Image 1

Background

UIPickerView is a common control in the Apple universe and making the life easy when to choose from a defined selection set as dates or data sets i.e. cars in my sample app. Such standard controls are supplying Voice Over which helps supported people with problems in using devices.

Using the code

The code is a simple sample for using the UIPickerView with a storyboard and code. For using the UIPickerView you need to add one in your storyboard and dont ferget to set the delegates. Delegation is a fundamental principle in Objective-C, without proper understanding and use your in trouble - and wont find out. As I understand it the delegate is like the message receiver and handler in Windows.

Fine is, that the Interface Builder Xcode supports it well. Here you see that the UIPickerView object has set the delegates (in the upper right corner) and its outlet. 

Image 2

Finally you need to implement the mandatory delegate functions to bring the UIPickerView to life.

Image 3

Points of Interest

Using the UIPickerView is as easy as using a table view. The MVC-model simplifies the data flow. 

A nice goodie is the Array loading via the plist. I know, that I will need it some day.

If anybody doesnt understand Interface Builder or the delegation he/she is lost.

And last but not least: Bruce Donnerson has enhanced his tutorial with a Part 2 on UIPickerViews. It contains a sample with 3 pickers.

History

- 18/2/16 Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)