UiPickerView Implementation in iOS Swift 3
Initially, you have to add the PickerView delegate and textField to the class.
Code:
}
Declare the picker in globally.
var Picker: UIPickerView!
var array:NSMutableArray!
In ViewDidLoad add the following code
Set the text field inputView as the picker.
textfield.inputView = Picker
Then Implement the PickerView delegate methods.
Picker = UIPickerView()
Picker.delegate = self
Set the text field inputView as the picker.
textfield.inputView = Picker
// MARK: - Picker view data source
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func numberOfRows(inComponent component: Int) -> Int{
return array.count
}
func pickerView(_ pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String?{
return array[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textfield.text = array[row]
}
// MARK: - TextField Delegate Methods
func textFieldDidEndEditing(_ textField: UITextField) {
textfield.text = array[row]
}
Comments
Post a Comment