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
Demo.swift
ContentView.swift
App.swift
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)
}
}
}
}
The example above shows a form with text, toggle, and picker controls bound to @State.
Segmented Picker & Stepper
Example
Demo.swift
ContentView.swift
App.swift
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)
}
}
}
Leave a Reply