SwiftUI Lists & Forms Inputs
SwiftUI Lists & Forms Inputs
Collect user input with TextField, Toggle, and Picker.
Collect input with TextField, Toggle, and Picker
Use form controls bound to @State to capture text, booleans, and choices.
Example
import SwiftUI
struct InputsDemo: View {
@State private var name = ""
@State private var enabled = true
@State private var choice = 1
var body: some View {
Form {
TextField("Name", text: $name)
Toggle("Enabled", isOn: $enabled)
Picker("Choice", selection: $choice) {
Text("One").tag(1)
Text("Two").tag(2)
}
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View { InputsDemo() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows a form with text, toggle, and picker controls bound to @State.
Segmented Picker & Stepper
Example
import SwiftUI
struct InputsAdvancedDemo: View {
@State private var selection = 0
@State private var count = 1
var body: some View {
Form {
Picker("Options", selection: $selection) {
Text("A").tag(0); Text("B").tag(1); Text("C").tag(2)
}
.pickerStyle(.segmented)
Stepper("Count: \(count)", value: $count, in: 1...5)
}
}
}
import SwiftUI
struct ContentView: View { var body: some View { InputsAdvancedDemo() } }
import SwiftUI
@main
struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
The example above shows a form with segmented picker and stepper controls bound to @State.