UiPickerView Implementation in iOS Swift 3


Initially, you have to add the PickerView delegate and textField to the class.
Code:

class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource,UITextFieldDelegate{
}



Declare the picker in globally.


var Picker: UIPickerView!
var array:NSMutableArray!

In ViewDidLoad add the following code

        Picker = UIPickerView()
        Picker.delegate = self

Set the text field inputView as the picker.

        textfield
.inputView = Picker

Then Implement the PickerView delegate methods.


    // 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.textarray[row]
    }

    //    MARK: - TextField Delegate Methods

    func textFieldDidEndEditing(_ textField: UITextField) {
        textfield.text = array[row]
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift