How to detect live changes on TextField in SwiftUI?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In SwiftUI, a TextField updates its bound value as the user types, which makes live change detection straightforward once you understand state and bindings. The usual pattern is to store the text in @State and react to updates with onChange.
Bind the TextField to State
The first step is to connect the field to a value SwiftUI can observe. As the user types, the bound String changes immediately.
This works because SwiftUI re-renders the view whenever username changes. The onChange modifier gives you a clean place to run validation, update helper text, enable buttons, or trigger lightweight calculations.
Use Live Changes for Validation and UI Feedback
Live updates are most useful when the response is cheap and local to the UI. Good examples include:
- showing a character count
- enabling a submit button only when input is valid
- formatting text as the user types
- showing inline hints
Here is a slightly richer example with a disabled button:
Notice that this second example does not even need onChange. Because the view derives its output directly from email, the UI still reacts live. That is an important SwiftUI idea: not every live update needs an explicit callback.
Avoid Heavy Work on Every Keystroke
A common mistake is to perform a network request or database search on each character. That makes the UI noisy and wastes work. When the reaction is expensive, debounce it.
One simple approach is to keep a reference to a Task, cancel the previous task, and wait briefly before running the expensive operation:
The cancellation step matters. Without it, an older search can finish after a newer one and briefly show stale results.
When to Use Other Hooks
Use onChange when you need side effects. Use derived properties when the UI can be computed directly from state. Use onSubmit only when you care about the return key or explicit submission, not every keystroke.
On newer platform versions you may also see onChange(of:initial:), which can run once on appearance and then on later changes. The core idea is still the same: SwiftUI watches a value and reruns your logic when that value changes.
Common Pitfalls
- Using
onSubmitwhen you actually need live typing updates.onSubmitdoes not fire on every character. - Doing expensive work for each keystroke without debounce or cancellation.
- Mutating the same value inside
onChangewithout a clear normalization rule. That can create surprising repeated updates. - Expecting the field to update a plain stored property. Live changes require a binding such as
@State,@Binding, or an observable model property. - Forgetting that many UI reactions do not need a callback at all. Derived state is often simpler and easier to test.
Summary
- A SwiftUI
TextFieldreports live edits through its bound state value. - '
onChangeis the standard tool when you need side effects during typing.' - Simple validation can often be expressed as derived UI state without any callback.
- Debounce expensive work so typing stays responsive.
- Choose
onSubmitonly for explicit submission events, not for per-character updates.
Related reading
- How to detect orientation change?
- How to detect orientation change?
- How to detect tableView cell touched or clicked in swift
- How to detect that animation has ended on UITableView beginUpdates/endUpdates?
- How to detect the end of loading of UITableView
- How to detect the end of UISlider drag?
- How to detect when a TextField loses the focus in SwiftUI for iOS?
- How to detect when a UIScrollView has finished scrolling
.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.