UITextField text change event
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting text changes in a UITextField is a common requirement for validation, live search, and enabling or disabling buttons. UIKit gives you several ways to do it, and the right choice depends on whether you want to react after the text changes or decide whether a change should be allowed at all.
The Simplest Option: .editingChanged
For most screens, target-action on .editingChanged is the cleanest approach. It fires after the field's text has been updated, which makes it ideal for live validation and UI refreshes.
This approach is simple because the updated text is already available on the field. You do not have to calculate it manually from a range and replacement string.
Use the Delegate When You Need Control
If you need to reject input before it is applied, use the delegate method textField(_:shouldChangeCharactersIn:replacementString:). This is useful for input masks, length limits, and character filtering.
Notice that this method runs before the field updates. If you want the future value, you have to compute it yourself as shown above.
Use Notifications When Another Object Cares
Sometimes the text field owner is not the component that needs to react. In that case, observing UITextField.textDidChangeNotification can be a reasonable choice.
This pattern is most useful when you are coordinating behavior across objects. If the view controller already owns the field, target-action is usually simpler.
Choosing the Right Hook
A practical rule is:
- use
.editingChangedfor normal live updates - use the delegate when you must approve or reject edits
- use notifications when an outside observer needs to listen
Keeping those use cases separate makes text-input code easier to read. It also avoids writing delegate code when the simpler control event would have been enough.
Common Pitfalls
- Using the delegate when
.editingChangedwould be simpler. - Forgetting that the delegate method sees the proposed change before it is applied.
- Reading
textField.textinsideshouldChangeCharactersInand assuming it is already updated. - Adding notification observers and never removing them in longer-lived objects.
- Mixing validation logic across target-action, delegates, and notifications without a clear reason.
Summary
- '
.editingChangedis the simplest way to react after the text changes.' - Delegate methods are best when you need to allow or reject edits.
- Notifications help when another object needs to observe changes.
- Compute the future value manually inside
shouldChangeCharactersIn. - Pick one primary mechanism for each field to keep input handling predictable.
Related reading
- UITextField text change event
- UITextField value changed not firing when field is updated
- UITextView content inset
- UITextView Disable selection, allow links
- UITextView style is being reset after setting text property
- UITextView that expands to text using auto layout
- UIView - How to get notified when the view is loaded?
- UIView bottom border?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.