Detect backspace in empty UITextField
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Detecting a backspace in an empty UITextField matters when the field is part of a custom input flow, such as an OTP screen or a token editor. The clean solution is not to guess from text changes after the fact, but to intercept the delete action directly.
Why the Delegate Method Is Not Enough
Many developers start with textField(_:shouldChangeCharactersIn:replacementString:). That method is useful for normal edits, but it is not the best hook when the field is already empty and you want special behavior such as moving focus to the previous field.
For regular deletion logic, it can still help:
That covers normal deletion, but it does not express the intent clearly when you specifically care about delete on an empty field.
The Robust Solution: Override deleteBackward()
UITextField exposes deleteBackward(), which is the correct place to detect backspace regardless of whether there is text in the field.
This works because you record whether the field was empty before the normal delete behavior runs. If it was empty, you trigger your custom callback after allowing UIKit to process the event.
A Practical OTP Example
The most common use case is a one-character code entry UI where pressing backspace in an empty field should move focus to the previous field.
This is much clearer than trying to infer empty-field backspace from delegate edge cases. It also keeps the control-specific behavior in the control subclass where it belongs.
Combine It with Normal Input Handling
Overriding deleteBackward() does not replace your normal text validation. You still use delegates or target-action for formatting, length limits, and advancing to the next field.
The delegate manages forward motion and text replacement. The subclass handles the empty-backspace case. The responsibilities stay separate and easier to test.
When You Need to Support Paste or Hardware Keyboards
OTP and token fields often receive paste events or input from hardware keyboards. deleteBackward() still works well for delete handling, but your delegate logic should also be ready for multi-character pastes.
That means validating replacementString, splitting pasted content if needed, and not assuming every input event is a single tap on the software keyboard.
Common Pitfalls
The most common mistake is trying to detect empty-field backspace only through shouldChangeCharactersIn. That can appear to work in one scenario and fail in another because it is not the most direct hook.
Another mistake is forgetting to call super.deleteBackward(). If you skip it, the field can behave differently from a normal UITextField.
Developers also capture self strongly in callbacks, which can keep the view controller alive longer than intended. Use [weak self] when the callback points back to the controller.
Finally, do not put every input rule inside the subclass. Keep the subclass focused on delete behavior and use delegates or view models for the rest of the form logic.
Summary
- The cleanest way to detect backspace on an empty
UITextFieldis to subclass and overridedeleteBackward(). - '
shouldChangeCharactersInis still useful for ordinary editing, but it is not the best abstraction for empty-field delete events.' - Empty-backspace handling is especially useful in OTP and multi-field entry UIs.
- Call
super.deleteBackward()so the control keeps UIKit's normal behavior. - Separate delete detection from the rest of your validation and focus-management logic.

