Here we will learn ios UI pickerview in swift with example and how to use ios UI control pickerview in swift to select a required item from the collection of items with an example using Xcode editor.
In iOS picker view will allow users quickly to choose the required option from multiple options by spinning a wheel on the screen. Picker views are well suited while choosing options between colors, font types, dates, times, etc. Generally, the picker view in iOS will be like as shown below.
By using UIPIckerView class reference in our application we can easily implement picker views based on our requirements. We will see how to use picker views in ios with example.
To create new project in iOS Xcode open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project
After selecting “Create a new Xcode project” a new window will open in that we need to choose template.
The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having a pre-configured interface and source code files.
For this iOS Picker View example, we will use the most basic template “Single View Application”. To select this one, Go to the iOS section on the left side à select Application à In the main area of dialog select “Single View Application” and then click on the next button like as shown below.
After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.
Product Name: “Picker View”
The name whatever we enter in the Product Name section will be used for the project and app.
Organization Name: “Tutlane”
You can enter the name of your organization or your own name or you can leave it as blank.
Organization Identifier: “com.developersociety”
Enter your organization identifier in case if you don't have any organization identifier enter com.example.
Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.
Language: “Swift”
Select language type as “Swift” because we are going to develop applications using swift.
Devices: “Universal”
Choose Devices options as Universal it means that one application is for all Apple devices in case if you have any specific requirement to run an app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.
Use Core Data: Unselected
This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.
Include Unit Tests: Unselected
In case if you need unit tests for your application then select this option otherwise unselect it.
Include UI Tests: Unselected
In case if you need UI tests for your application then select this option otherwise unselect it.
Once you finished entering all the options then click on Next button like as shown below
Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below
After clicking on the Create button, the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which we used to design the app user interface and to maintain source code.
Main.storyboard - Its visual interface editor and we will use this file to design our app user interface
ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.
Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.
Now select ViewController.swift file in your project that view will be like as shown below.
Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode on the right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively you can choose View à Utilities à Show Object Library).
As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for the picker view in the Filter field then drag and drop the picker view control into Main.storyboard ViewController same way add two label controls to ViewController like as shown below.
Now we will make connection between controls and ViewController.Swift code for that click on assistant button (overlap circle) in Xcode toolbar right side corner like as shown below
To map the controls, press the Ctrl button in keyboard and drag the picker view and label controls from the canvas interface and drop into ViewController.swift file like as shown below
Once we add controls to ViewController.swift file then write the custom code to bind values to picker view and get selected option value from picker view. Once we write all required functionality our ViewController.swift file code should be like as shown below
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// color and size array
var colorList = ["(( Select ))", "Black", "Blue", "Brown", "Green", "Orange", "Pink" , "Purple", "Red", "Yellow"]
var sizeList: [String]!
// outlet - color and size label
@IBOutlet var colorLabel: UILabel!
@IBOutlet var sizeLabel: UILabel!
//outlet - picker view
@IBOutlet var pickerView: UIPickerView!
// MARK: - view functions
override func viewDidLoad() {
super.viewDidLoad()
// set picker view delegate
self.pickerView.dataSource = self
self.pickerView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - picker view delegate and data source
// how many component (i.e. column) to be displayed within picker view
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
// How many rows are there is each component
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return self.colorList.count
}
return 0
}
// title/content for row in given component
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return self.colorList[row]
}
if component == 1 {
return self.sizeList[row]
}
return "Invalid Row"
}
// called when row selected from any component within a picker view
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
// if first row then set --
if row == 0 {
self.colorLabel.text = "--"
}else{
self.colorLabel.text = self.colorList[row]
}
}
}
}
Now we will run and see the output of iOS Delegates application. To run application, first select required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.
Now rotate the Picker View the respective selected color value will shown in the label.
This is how we can use iOS UI pickerview in swift to choose the required option from the collection of items based on our requirements in Xcode.