Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
quick and dirty dynamic pickerView in Swift 3
//
// ViewController.swift
// picker
//
// Created by Slawomir Jasinski on 30/11/16.
// Copyright © 2016 Slawomir Jasinski. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
// text fields
@IBOutlet var tf_A: UITextField!
@IBOutlet var tf_B: UITextField!
// content for pickerViews
let optionsA = ["Sunday" ,"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
let optionsB = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
// variables to gold current data
var picker : UIPickerView!
var activeTextField = 0
var activeTF : UITextField!
var activeValue = ""
override func viewDidLoad() {
super.viewDidLoad()
// delegates for text fields
tf_A.delegate = self
tf_B.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// number of components in picekr view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// return number of elements in picker view
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// get number of elements in each pickerview
switch activeTextField {
case 1:
return optionsA.count
case 2:
return optionsB.count
default:
return 0
}
}
// return "content" for picker view
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// return correct content for picekr view
switch activeTextField {
case 1:
return optionsA[row]
case 2:
return optionsB[row]
default:
return ""
}
}
// get currect value for picker view
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// set currect active value based on picker view
switch activeTextField {
case 1:
activeValue = optionsA[row]
case 2:
activeValue = optionsB[row]
default:
activeValue = ""
}
}
// start editing text field
func textFieldDidBeginEditing(_ textField: UITextField) {
// set up correct active textField (no)
switch textField {
case tf_A:
activeTextField = 1
case tf_B:
activeTextField = 2
default:
activeTextField = 0
}
// set active Text Field
activeTF = textField
self.pickUpValue(textField: textField)
}
// show picker view
func pickUpValue(textField: UITextField) {
// create frame and size of picker view
picker = UIPickerView(frame:CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.size.width, height: 216)))
// deletates
picker.delegate = self
picker.dataSource = self
// if there is a value in current text field, try to find it existing list
if let currentValue = textField.text {
var row : Int?
// look in correct array
switch activeTextField {
case 1:
row = optionsA.index(of: currentValue)
case 2:
row = optionsB.index(of: currentValue)
default:
row = nil
}
// we got it, let's set select it
if row != nil {
picker.selectRow(row!, inComponent: 0, animated: true)
}
}
picker.backgroundColor = UIColor.white
textField.inputView = self.picker
// toolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.barTintColor = UIColor.darkGray
toolBar.sizeToFit()
// buttons for toolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
textField.inputAccessoryView = toolBar
}
// done
func doneClick() {
activeTF.text = activeValue
activeTF.resignFirstResponder()
}
// cancel
func cancelClick() {
activeTF.resignFirstResponder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.