iOS / Swift / Concurrency and data boundaries / Lesson 14
Focus state and form correction
Direct SwiftUI focus to the appropriate field while keeping validation, accessibility and submission separate.
On this page
Lesson goals
- Bind distinct fields to FocusState values.
- Move and clear focus deliberately.
- Verify correction and keyboard behavior on a target.
Define the focus contract
A form should help the user find the field that needs correction without losing the draft. FocusState reflects focus in a SwiftUI scene and can also request a focus change. Decide when moving focus helps rather than surprising the user. Keyboard focus, VoiceOver focus, validation and server authorization are related concerns but are not interchangeable.
Bind distinct fields
The sample gives Name and Email separate enum cases and moves focus to the first empty field when Check is activated. It intentionally demonstrates focus only: a real form needs visible error messages, full validation and an authorized submission. The view requires a SwiftUI host and compatible SDK; no target execution is claimed.
import SwiftUI
struct ContactForm: View {
enum Field: Hashable { case name, email }
@State private var name = ""
@State private var email = ""
@FocusState private var focused: Field?
var body: some View {
Form {
TextField("Name", text: $name).focused($focused, equals: .name)
TextField("Email", text: $email).focused($focused, equals: .email)
Button("Check") {
if name.isEmpty { focused = .name }
else if email.isEmpty { focused = .email }
}
}
}
}
Checking an empty form requests focus for Name.
Distinct focus values avoid ambiguity when selecting the field needing correction.
Clear focus deliberately
A Boolean FocusState can represent one focused field. Setting it to false removes focus from that bound field, which may dismiss the software keyboard. Here Done changes only focus and does not save the note. Preserve the draft when focus changes and communicate any actual submission result separately.
import SwiftUI
struct NoteField: View {
@State private var note = ""
@FocusState private var focused: Bool
var body: some View {
VStack {
TextField("Note", text: $note).focused($focused)
Button("Done") { focused = false }
}
}
}
Done requests removal of focus from Note.
The button clears the fieldโs focus state without changing its text.
Avoid ambiguous bindings
Do not bind the same enum case to two different fields when programmatically selecting one; SwiftUI cannot infer which field the application intended. If an asynchronous validation completes after navigation, check that the owning view and input are still current before changing focus. Provide error text and accessible context instead of relying on a focus movement as the only explanation.
Verify the interaction
Test an empty first field, a populated first field with an empty second field and a valid draft. Exercise hardware keyboard navigation and the software keyboard on supported targets, then verify VoiceOver behavior independently. Confirm the draft survives focus changes and that a delayed result cannot move focus in a replaced screen. Record the SDK and target for each result.
Common errors and fixes
Focus moves to the wrong field.
- Why it happens
- Two fields use the same focus value.
- How to fix it
- Assign a distinct enum case to each target.
- Prevent it next time
- Test each validation branch.
The user cannot understand why focus moved.
- Why it happens
- A focus change was used as the only error feedback.
- How to fix it
- Show a clear error associated with the field.
- Prevent it next time
- Test correction with keyboard and VoiceOver.
Focus changes after leaving the form.
- Why it happens
- A delayed task published to an obsolete owner.
- How to fix it
- Check task and view ownership before applying the result.
- Prevent it next time
- Test navigation during validation.
2 practice exercises
1. Why use separate enum cases for Name and Email?
Show worked solution
Each case identifies one focus target. Reusing a value for multiple fields makes programmatic focus selection ambiguous.
2. Does setting focus to false submit the note?
Show worked solution
No. It removes focus from the bound field. Submission and confirmation require their own explicit operation.
Key takeaways
- Give each programmatic focus target a distinct value.
- Keep draft, feedback and submission separate.
- Verify keyboard and assistive interaction on the target.