SwiftUI Data Flow @Binding

@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()
} }

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *