Created
August 3, 2018 13:21
-
-
Save SashaTsebrii/1a411061ea9703f438ca35f2fc836e3f to your computer and use it in GitHub Desktop.
Picker View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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