Honey Honey - 2 months ago 15
iOS Question

To dismiss keyboard is there any difference between using `resignFirstResponder` and `endEditing`?

I read this question: How to hide keyboard in swift on pressing return key?

and basically there are 2 ways to answer:


  • self.view.endEditing(true)

  • textField.resignFirstResponder()



They both get the job done independently. But do they have any other difference/caveats or places that one one work and the other won't?

Answer

self.view.endEditing(true) is less efficient because it will cycle through the entire view hierarchy and makes sure anything that can be a firstRepsonder has resigned it.

If you know which item is currently the responder, it is more efficient and better practice to call resignFirstResponder on it directly.