@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
Demo.swift
ContentView.swift
App.swift
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()
}
}
Leave a Reply