27

What exactly are the differences between the UIControlEvents .EditingDidEnd and .EditingDidEndOnExit, and how do they relate to each other?

The documentation on UIControl is pretty vague, and has no information of whether these fire for different reasons, if one is a superset of the other, or if they're functionally equivalent.

The possibility of one being a superset is especially confusing, since the naming implies that .EditingDidEndOnExit is a specific occurrence of .EditingDidEnd, but the documentation seems to imply the opposite.

Kazmasaurus
  • 608
  • 7
  • 12

3 Answers3

35

".EditingDidEnd" is called when somebody touches outside of the bounds of the text field, because they're likely about to interact with some other control or object.

"EditingDidEndOnExit" is called when the user clicks the "return" key in the keyboard (and you're right, it's not clear in the documentation... but if you look at the UIControl.h file you'll see a comment reflecting this point).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • `EditingDidEndOnExit` does dismiss the keyboard. But `EditingDidEnd` seems dismiss if I click in another text field, but not in any-other location (off-a-field) – benc Oct 06 '18 at 06:56
  • 1
    If I have a UITextField with both, I see tapping the `return` triggering both... like `EditingDidEndOnExit; EditingDidEnd`. When I add logging to wrap the target action, they seem to be run on separate threads, because the output is interlaced. – benc Jan 10 '19 at 18:07
9

From the UIControl header:

UIControlEventEditingDidEnd       = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing

So one is when the return key was pressed, the other is from touching outside of the bounds.

Logan
  • 52,262
  • 20
  • 99
  • 128
0

When a user taps on the return key of the keyboard, the following events are executed in order:

  • editingDidEndOnExit
  • primaryActionTriggered
  • editingDidEnd

When a user taps another UITextField or UITextView object, only 'editingDidEnd' is executed.

When app code executes 'resignFirstResponder()' on a given UITextField, only 'editingDidEnd' is executed.

When app code executes 'endEditing(true|false)' on a given UITextField, only 'editingDidEnd' is executed.

When app code executes 'becomeFirstResponder()' on another UITextField, only 'editingDidEnd' is executed.

When a user taps on the return key of the keyboard and textField has a delegate, the following events are executed in order:

  • delegate: textFieldShouldReturn
  • editingDidEndOnExit
  • primaryActionTriggered
  • editingDidEnd
  • delegate: textFieldDidEndEditing

Keyboard will dismiss if one or both following events will be supported:

  • textField.addTarget(self, action: #selector(editingDidEndOnExit), for: .editingDidEndOnExit)
  • textField.addTarget(self, action: #selector(primaryActionTriggered), for: .primaryActionTriggered)

Checked on iOS 16.2

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93