SwiftUI Data Flow @Binding
SwiftUI Data Flow: @Binding
Use @Binding to pass a reference to a parent's state into a child view so updates flow back.
Bind child input to parent state
Bind a parent's state to a child input so edits update the parent value in real time.
Example
import SwiftUI
struct Child: View {
@Binding var text: String
var body: some View {
TextField("Enter", text: $text)
.textFieldStyle(.roundedBorder)
}
}
struct Parent: View {
@State private var name = "Kai"
var body: some View {
VStack {
Text(name)
Child(text: $name)
}
.padding()
}
}
import SwiftUI
struct ContentView: View {
var body: some View { Parent() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows a parent view that passes its state to a child view, which updates the parent in real time.