How to hide the keyboard using a picker?

I want to fill some textfields using the normal keyboard and a pickerview.

The problem is, that the keyboard doesn´t hide after klicking on the textfield for the pickerview.

I used self.view.endEditing(true) in the function textFieldDidBeginEdeting to hide the keyboard.

Replies

You can do it within the IBAction of the textField Editing done :


    @IBAction func textFieldDoneEditing(sender: UITextField) { 
        sender.resignFirstResponder()
    }


or implement the received action for the view :

    @IBAction func backgroundTapped(sender: UIControl) {
        // You may save data as needed here
        theTextField.resignFirstResponder() 
    }

I tried it with the resignFirstResponder, but the keyboard doesn´t hide.

I´m typing in an other textfield and want to hide the keyboard, when i go into the textfield for the pickerview.

Otherwise the keyboard is hiding my pickerview.

Maybe the code will help.



class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var pickerTF: UITextField!

@IBOutlet weak var pickerPV: UIPickerView!

@IBOutlet weak var keyboardTestTF: UITextField!


override func viewDidLoad() {

super.viewDidLoad()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

var test = ["1", "2", "3"]

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1

}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

return test.count

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

return test[row]

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

self.pickerTF.text = self.test[row]

self.pickerPV.isHidden = true

}


func textFieldDidBeginEditing(_ textField: UITextField) {

if textField == self.pickerTF {

self.pickerPV.isHidden = false

textField.endEditing(true)

}

}

//Action for the textfield for typing with the Keyboard

@IBAction func hideKeyboardFromTF(_ sender: UITextField) {

sender.resignFirstResponder()

}

}

You don't have defined textFieldDoneEditing


Include this

    @IBAction func textFieldDoneEditing(sender: UITextField) {
        sender.resignFirstResponder()
    }

That may be redundant, but it's good practice anyway


I think your problem is here :

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == self.pickerTF {
            self.pickerPV.isHidden = false
            textField.endEditing(true)
        }
    }

textField == self.pickerTF, so you endEditing on this, not on the initial textField you were editing.

replace by

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == self.pickerTF {
            self.pickerPV.isHidden = false
            theTextFieldToWhichTheKeyboadIsAssociated.endEditing(true)
        }
    }

As it is not obvious from the code to know which textField is which, I've written : theTextFieldToWhichTheKeyboadIsAssociated

Replace by the right one, probably keyboardTestTF ??

I added the new action, but it doesn´t work.

I also used the given function textFieldDidEndEdeting.

@IBAction func textFieldDoneEdeting(_ sender: UITextField) {
     sender.resignFirstResponder()
}


I also tested textFieldwithKeyboardTF.endEditing(true), but the keyboard is again hiding my pickerview.func textFieldDidBeginEditing(_ textField: UITexsField) {a

     if textField == self.textFieldwithPickerViewTF {
          self.pickerPV.isHidden = false
          textField.endEditing(true)
          //textFieldwithKeyboardTF.endEditing(true)
     }
}


The line textField.endEditing(true) is needed to hide the Keyboard for the pickerview textfield.

This works, until there is a Keyboard on the screen.

I don't know if you have the typo in the code itself:

textFieldDoneEdeting


is not the right one ; should be

textFieldDoneEditing // i, not e


It seems to me that with what you write :

if textField == self.textFieldwithPickerViewTF { 
          self.pickerPV.isHidden = false 
          textField.endEditing(true) 
          //textFieldwithKeyboardTF.endEditing(true) 
     }

you don't close the keyboard opend with the first textField

you need an endEditing for this one as well

Is TextFieldDoneEditing a given function?

I can´t find this function.

You have to declare an IBAction textFieldDoneEditing and connect the event Did End On Exit of the textField to this IBAction

Doesn´t work again, I´m turning crazy.


But I´m using button now to get the information and i have no problems with the keyboard.

Everything is working now:)