1

I have a UITextField that can be set into editing mode.

Normally, editing is ended by entering „Done“ on the keyboard. This calls textFieldDidEndEditing(_ textField: UITextField), where some housekeeping happens.

In rare cases, however, an alert is shown that the user entered a geofence. If the text field is then in editing mode, it resigns first responder status since the alert view becomes first responder, and textFieldDidEndEditing(_ textField: UITextField) is also called. When the alert is dismissed, the first responder status of the text field is restored.

The problem:
I have to distinguish both cases:
If the text field ends editing because of a Done on the keyboard, housekeeping should happen.
If it ends editing because an alert was shown, housekeeping must not be done.

So, how can I distinguish both cases?

I tried to use the delegate function

textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason)

where reason may be cancelled or committed, both in both cases the reason is committed.

AVerguno
  • 1,277
  • 11
  • 27
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116

1 Answers1

2

You could use textFieldShouldEndEditing and assuming you can identify that the geofence has been triggered, return false, which will leave the textField in edit mode, otherwise return true, and the housekeeping can begin

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool
{
    // assuming you take the same action for all TextFields
    // assume geoFenceHasBeenTriggered is a class-variable true / false
    return !geoFenceHasBeenTriggered
}
Russell
  • 5,436
  • 2
  • 19
  • 27