Easy Setup PickerView In iOS

Posted By : Vasu Saini | 27-Oct-2017

Picker View in iOS is sometimes a hectic module when it is used multiple times on same controller or same screen specially if the controller is designed on tableview. let us suppose that if we have designed a signup page have different fields like gender, country, state, city such all that fields having picker views.

Here as shown on in figure below there are various fields designed on table view on which we have to open picker view such as gender, city etc
There are different ways to handle such type of things. let us discuss about it.

 

 

Step 1. Create / Open project in which you want to do such stuff.

Step 2. Install Pod  mentioned below if you are using Swift 4 otherwise remove branch from it.

pod 'SwiftyPickerPopover', :git => 'https://github.com/hsylife/SwiftyPickerPopover', :branch => 'swift4'

Step3.Now create a new swift file and name it as Picker.swift

Step 4. Now Paste the code as shown below in the picker.swift

 

//
//  Picker.swift
//  EimarsMLM
//
//  Created by Vasu Saini on 04/10/17.
//  Copyright © 2017 oodlesTechnologies_Vasu_Saini. All rights reserved.
//

import Foundation
import UIKit
import SwiftyPickerPopover

class Picker {

    static func showPickerWith(_ title: String, _ items: Array<String>, _ view: UIView, _ controller: UIViewController, completion: @escaping (_ selectedString: String, _ selectedIndex : Int) -> Void) {

        let picker = StringPickerPopover(title: title, choices: items)
            .setDoneButton(action: {
                popover, selectedRow, selectedString in
                completion(selectedString, selectedRow)
            })
        picker.appear(originView: view, baseViewController: controller)
    }

}

Step 5. This is class to the third party used to call picker

Step 6. Now Open view controller class in which you want to use this picker view.

Step 7. Call that picker view function as follows.

 

 

        Picker.showPickerWith("Title", ["Item 1", "Item 2"], textFieldPassed, self) {  (string, selectedIndex) in

          print("Selected value = \(string)")  

          print("Selected index = \(selectedIndex)")  

 

 

        }

Step 8.All you have setup pass the title  and array of elements you want to give to your picker as shown in above image.

Step 9. All Done

About Author

Author Image
Vasu Saini

Vasu Saini is Passionate to deploy ideas into real world, Have zeal to learn new technologies. working as iOS Developer in Oodles Technologies. Sports Freak, Calisthenics ,Parkour, Love to play football, swimmer, athletics etc

Request for Proposal

Name is required

Comment is required

Sending message..