Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SashaTsebrii/1a411061ea9703f438ca35f2fc836e3f to your computer and use it in GitHub Desktop.
Save SashaTsebrii/1a411061ea9703f438ca35f2fc836e3f to your computer and use it in GitHub Desktop.
Picker View
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var theTextfield: UITextField!
let myPickerData = [String](arrayLiteral: "Peter", "Jane", "Paul", "Mary", "Kevin", "Lucy")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let thePicker = UIPickerView()
thePicker.delegate = self
theTextfield.inputView = thePicker
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UIPickerView Delegation
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myPickerData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myPickerData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
theTextfield.text = myPickerData[row]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment