Mobile development

Stage 2 · Lesson 6 of 10

@State and bindings

SwiftUI views and layout

@State marks a value that a view owns, such as whether a sheet is showing or the text in a field. It is local UI state. stores it outside the view and redraws the view when it changes.

Mark it private, because it belongs to that view alone. Sometimes a child view needs to change a value its parent owns. Then the child takes a @Binding, a two-way link to that value.

The parent passes it with the $ prefix, as in Toggle("Wi-Fi", isOn: $isOn). Built-in controls like TextField and Toggle all work through . Sharing state between screens with @Observable comes in the next stage.

Common trapgiving a child its own @State copy of the parent's value, so the two quietly drift apart.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CourseFree
    SwiftUI Tutorials (Apple)

    Apple · Beginner

    Apple's own path from views and stacks to lists and navigation

  • CourseFree
    100 Days of SwiftUI

    Paul Hudson (Hacking with Swift) · Beginner

    Days 16 to 35 build six small apps on views, modifiers, lists and animation

Practise

  • Use a free challenge's mobile design image as the spec for a native screen

Keep handy

Mark this stage as not done?