Creating a Picker View in iOS Swift with Multiple Selection
Introduction
When it comes to selecting multiple items from a list, the UITableView and its related classes can be a bit cumbersome. However, Apple provides an alternative solution through the UIPickerView. In this article, we’ll explore how to create a UIPickerView with multiple selection in iOS using Swift.
Prerequisites
Before diving into the implementation, make sure you have:
- Xcode 11 or later installed on your machine.
- A basic understanding of Swift programming language and iOS development.
Understanding UIPickerView
A UIPickerView is a view that allows users to select items from a list. Unlike UITableView, which uses rows to display data, UIPickerView displays data as separate components called “components”. Each component represents an item in the list.
Creating a Picker View with Multiple Selection
To create a picker view with multiple selection, we’ll need to follow these steps:
Step 1: Import Objective-C
We can import Objective-C code into our Swift project using Bridging the Code feature in Xcode. To do this:
- Open your project in Xcode.
- Go to File > New > File…
- In the new file dialog, select Objective-C Header File (.h) and click Next
- Name your file (e.g.,
bridgingHeader.h) and click Create
This step creates a new Objective-C header file that will be used to import our Swift code.
Step 2: Add Bridging Header
After creating the bridging header, Xcode will prompt you to create a bridging header file. To do this:
- Open your project in Xcode.
- Go to File > Project Settings
- In the Build Settings, find the Framework Search Paths setting and add the path to your
bridgingHeader.hfile
This step tells Xcode where to look for our Swift code when importing Objective-C.
Step 3: Import Objective-C Code
We’ll now import our Objective-C code into our Swift project. To do this:
- Open your bridging header file (
bridgingHeader.h) and add the following line:
#import <UIKit/UIKit.h>
* This imports the UIKit framework, which includes all the classes and protocols used in iOS development.
### Step 4: Create a Picker View
We'll now create our picker view using the `UIPickerView` class. To do this:
* Open your main storyboard file (e.g., `Main.storyboard`) and drag a new object onto the scene.
* Select **UIPickerView** from the object library
* Set the **Picker Style** to **Picker Style** and the **Component Count** to 10
This step creates our picker view with two components.
### Step 5: Implement Picker View Delegate
To handle the selection of items in our picker view, we'll implement the `UIPickerViewDelegate` protocol. To do this:
* Open your main storyboard file (e.g., `Main.storyboard`) and select the **UIPickerView** object
* Go to **Identity Inspector**
* In the **Delegate** field, click on the **Connect** button
* Select the **ViewController** class
This step sets our view controller as the delegate for our picker view.
### Step 6: Implement Picker View Data Source
To handle the selection of items in our picker view, we'll implement the `UIPickerViewDataSource` protocol. To do this:
* Open your main storyboard file (e.g., `Main.storyboard`) and select the **UIPickerView** object
* Go to **Connection Inspector**
* In the **Data Source** field, click on the **Connect** button
* Select the **ViewController** class
This step sets our view controller as the data source for our picker view.
### Step 7: Implement Picker View Selection Handler
To handle the selection of items in our picker view, we'll implement the `pickerView(_:selectedRow ForComponent:)` method. To do this:
* Open your **ViewController.swift** file and add the following code:
```swift
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Set up picker view data source and delegate
pickerView.dataSource = self
pickerView.delegate = self
// Set up picker view component count
pickerView.componentsCount = 10
}
// MARK: - Picker View Data Source Methods
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
// MARK: - Picker View Delegate Methods
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "Item \(row)"
}
}
- This code sets up our picker view data source and delegate, as well as its component count.
Step 8: Run the App
To run the app and see our picker view in action:
- Build and run your project
- Select an item from the picker view to see it highlighted
Conclusion
In this article, we explored how to create a UIPickerView with multiple selection in iOS using Swift. We covered the basics of UIPickerView, implemented the delegate and data source protocols, and set up our picker view components.
I hope this helps you understand how to use UIPickerView in your own iOS apps!
Last modified on 2024-09-24