Search code examples
iosswift3uitextfielduipickerview

How to add or replace textfield input text to picker view text in Swift 3


enter image description here

I have a picker loaded with an array of strings, I want to change means appending or editing strings by typing in the text field and save in that array and display that changes in that picker view.Thanks in advance.


Solution

  • extension ViewController: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        addIntoArray(text: textField.text)
    }
    
    func addIntoArray(text: String?) {
        guard let text = text, text.count > 0 else {
            //for blannk or nil string
            return
        }
    
        listArray.append(text)
        //reload the picker
        picker.reloadAllComponents() //picker is the instanc of your pickerView
    }   
    }
    

    try this