← All daysDay 25 of 49 · Part 7: Input and interaction
Day 25

Controls and forms · Text input and focus

These are the lesson notes for this day. Its build-along tutorial and flash cards have not been written yet.

Lesson 28

Controls and forms

SwiftUI's controls — Button, Toggle, Picker, Slider, Stepper, DatePicker — each bind to a piece of state, and each changes its appearance to suit where it is placed. A Form groups controls into the standard settings layout and styles each one appropriately. You change how a control looks with a style: .buttonStyle(.borderedProminent), .pickerStyle(.segmented). You write your own by conforming to a protocol such as ButtonStyle.

Coming from UIKit: there are no target–action pairs and no delegates. A control is bound to a value. When the user changes the control, the value changes, and the views that depend on it update.

Exercise

Build a settings screen in a Form with a toggle, a segmented picker, a stepper, and a slider, all backed by one @Observable settings object. Write a custom ButtonStyle for its Save button.

After this lesson you should be able to answer
  1. How does a Toggle report the user's change to your code?
  2. What does Form do for the controls inside it?
  3. How do you give every button on a screen the same custom appearance?
Resources
Lesson 29

Text input and focus

TextField binds to a string, or to another type if you give it a format: (for numbers and dates). SecureField hides what is typed. TextEditor handles several lines and, since iOS 26, rich text through AttributedString. You configure the keyboard with .keyboardType, .textContentType, .textInputAutocapitalization and .submitLabel, and you respond to the Return key with .onSubmit.

Focus is state as well. A @FocusState property holds which field has the keyboard. You connect fields to it with .focused(), and you move focus by assigning to it. That replaces becomeFirstResponder.

Exercise

Build a login form with two fields. Pressing Return in the first field moves focus to the second; pressing Return in the second submits. The Submit button is disabled until both fields are valid.

After this lesson you should be able to answer
  1. How do you move keyboard focus from one field to another in code?
  2. How do you bind a TextField to a number instead of a string?
  3. How do you run code when the user presses Return?
Resources