Model SwiftUI screen state
Decide where user-editable screen state belongs and reflect its changes in SwiftUI.
Keep a single source of truth
A SwiftUI view describes what to display from the current value of its state. For a draft used only by a single editing view, @State can give the view owned local state. If a child needs to edit the parent’s value, a binding can pass a way to read and update the same value.
How it works
Start with one piece of state, such as whether a reading item is marked complete. The view describes what to show for its current value. An action changes the state, and the view updates from that value so the interface and model share the same source of truth.
Separate drafts from saved records
An editing screen can store the current input as a draft and update the saved model only after validation. Cancel discards that draft. Test these behaviors and make sure larger text sizes leave room for the labels and controls.
Further reading
Check your understanding
A user changes a title and taps Cancel. Which title should the list show?
See the example and explanation
- Example 1
- Given
Saved: Maps. Draft: Stars. Action: Cancel. - Answer
Maps
Show the saved title. Cancel discards the draft, so the edit never updates the stored record.