In Swift, both [someTextField].resignFirstResponder()
and self.view.endEditing(true)
accomplish the same task - hiding the keyboard from the user's view and de-focusing whatever text field was using it. I understand that the former is specific to a particular field, while the latter encompasses the entire view, but other than wanting to target a specific text field, when is one preferred/recommended over the other?

- 6,736
- 11
- 45
- 55

- 857
- 1
- 6
- 9
-
1i do not know anything about specific cases to use the one or the other. i would say that if you have a reference to the textfield you should use `resignFirstResponder`; otherwise it's totally ok to call `endEditing` on the view. feel free to correct me :) – André Slotta Apr 26 '15 at 20:02
-
It's a really interesting question. I **cannot see any reason** to ever use `resignFirstResponder.` I believe they will deprecate `resignFirstResponder.` in the future. (Regarding the efficiency aspect, you're talking a handful of items, it's irrelevant ... if for some reason you happen to want to, you can call `endEditing` on just that one item.) – Fattie Jan 21 '17 at 12:58
2 Answers
someTextField.resignFirstResponder()
resignFirstResponder()
is good to use any time you know exactly which text field is the first responder and you want to resign its first responder status. This can be slightly more efficient then the alternative, but if you're doing something such as creating a custom control, this can make plenty of sense. Perhaps you have a text field, and when the "Next" button is pressed, you want to get rid of the keyboard and present a date picker, for example. Here, I would definitely use resignFirstResponder()
self.view.endEditing(true)
I typically reserve this for scenarios when I just absolutely need to clear the keyboard no matter what is currently going on, for whatever reason. Perhaps, I've got a slide-over menu? Just before this slides out, no matter what is going on, the keyboard should go away, so I'll make sure everything resigns its first responder status. It's important to note that endEditing()
will look through the entire hierarchy of subviews and make sure whatever is the first responder resigns its status. This makes it less efficient then calling resignFirstResponder()
if you already have a concrete reference to the first responder, but if not, it's easier than finding that view and having it resign.
-
Ahhh, there's what I was wondering, if there was any underlying difference that was not readily apparent. Thanks for the help! – curiouslychris Apr 26 '15 at 22:50
-
Actually, as a follow-up to your answer...in a scenario where the first responder might not be known, is there a function that can find out what object currently has focus, then provide that object's name to `resignFirstResponder()`? Seems it would be more efficient than the second method, if it's possible. – curiouslychris Apr 26 '15 at 23:12
-
If you don't know the object, calling `endEditing` is going to be more efficient then trying to figure out which object is the first responder just to call `resignFirstResponder` on it. – nhgrif Apr 26 '15 at 23:16
-
@nhgrif we can use `resignFirstResponder()` for UIView. what does that mean? – nitish005 Apr 19 '16 at 06:47
-
if I have multiple textFields like: firstName, LastName, Address, etc. and then at the end I have a **`submit`** button, would doing `self.view.endEditing` be a better choice – mfaani Sep 21 '16 at 18:41
-
Assume you know the first responder and have a reference to it. Which should you call, `resignFirstResponder()` or `endEditing(true)`? – Andrew Kirna Jul 24 '19 at 19:12
There is no such strict rule.
You use resignFirstResponder
when you have the reference of the text field currently holding first responder status. When you don't have the reference or you are unsure about that, endEditing
would do the trick.
One thing however should be noted that endEditing
has a boolean parameter that we occasionally set to true
. By setting this parameter to true
the view, on which endEditing
has been called, will force every child text field to resign first responder status irrespective of it has returned a false
value from textFieldShouldEndEditing
delegate method. On the contrary calling endEditing
with false
would only ask (not force) the text field to resign, respecting the return value from textFieldShouldEndEditing
protocol method.

- 5,238
- 2
- 28
- 40
-
Gotcha, that was a method I was not aware of...I'm guessing that would be used in text validation or other error handling? – curiouslychris Apr 26 '15 at 22:52