Question

Comment puis-je masquer le clavier lorsque l'utilisateur clique sur un bouton? Un court exemple pour mieux comprendre: l'utilisateur édite un texte dans certains champs de texte et à la fin il ne cliquez pas sur « Done » ou smething autre sur le clavier, mais il clique sur un bouton « Enregistrer », tandis que le clavier est toujours affiché . Alors, comment puis-je rejeter maintenant le clavier?

Thx. Cordialement, Daniel

Était-ce utile?

La solution

in button action write,

if([textField isFirstResponder]){
  [textField resignFirstResponder];
}

if there are more textfields get the current textfield reference everytime while editing started, and resign its responder in button action.

Autres conseils

You can also call [view endEditing:YES] on any view above the text field in the hierarchy. This is useful if you aren't sure which text field has first responder status. It also optionally lets a text field delegate stop the action (by returning NO from shouldEndEditing:) which is nice if you are using a delegate to do validation on the fields.

If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing: on the parent view containing the text fields.

In Swift, in the related view controller, add this line to the button function:

self.view endEditing(true)

In addition to SriPriya's answer, please have here the Swift 2 styled version:

if numberTextField.isFirstResponder() {
    numberTextField.resignFirstResponder()
}

Please enjoy...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top