@Binding lets a child view read and write a value owned by its parent. This helps keep data in sync between views.
@Binding for child communication in iOS Swift
struct ChildView: View {
@Binding var isOn: Bool
var body: some View {
Toggle("Switch", isOn: $isOn)
}
}Use @Binding in the child to declare a variable that links to the parent's state.
Pass the binding from the parent using the $ prefix.
isOn state and passes a binding to the child.struct ParentView: View {
@State private var isOn = false
var body: some View {
ChildView(isOn: $isOn)
}
}count state through the binding.struct ChildView: View {
@Binding var count: Int
var body: some View {
Button("Increment") {
count += 1
}
}
}This app shows a toggle switch controlled by the child view. The parent displays the current state text. Changing the toggle updates the parent's state instantly.
import SwiftUI struct ParentView: View { @State private var isOn = false var body: some View { VStack { Text(isOn ? "Switch is ON" : "Switch is OFF") .font(.title) .padding() ChildView(isOn: $isOn) .padding() } } } struct ChildView: View { @Binding var isOn: Bool var body: some View { Toggle("Toggle Switch", isOn: $isOn) .padding() } } struct ContentView: View { var body: some View { ParentView() } }
Always prefix the parent's state with $ when passing to a @Binding variable.
Bindings keep data in sync both ways: child changes update parent and parent changes update child.
Use @State in the parent and @Binding in the child for this pattern.
@Binding lets child views read and write parent state.
Pass bindings from parent to child using $ prefix.
This keeps data synchronized between views easily.
Practice
@Binding in SwiftUI child views?Solution
Step 1: Understand @Binding role
@Binding creates a two-way connection allowing child views to read and write to a parent's state.Step 2: Compare with other options
Other options describe independent state or one-way data flow, which @Binding does not do.Final Answer:
To allow child views to read and modify a parent's state directly -> Option BQuick Check:
@Binding = two-way parent-child state link [OK]
- Confusing @Binding with @State creating independent state
- Thinking @Binding only passes data one-way
- Assuming @Binding prevents child from modifying data
Solution
Step 1: Identify correct binding syntax
In SwiftUI, to pass a binding, use the $ prefix on the parent's @State variable.Step 2: Validate options
ChildView(binding: $parentState) correctly uses$parentStateto pass a binding; others misuse $ or omit it.Final Answer:
ChildView(binding: $parentState) -> Option DQuick Check:
Use $ to pass binding from parent [OK]
- Forgetting the $ prefix when passing binding
- Using $ incorrectly in parameter name
- Passing state value instead of binding
struct ParentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
ChildView(count: $count)
}
}
}
struct ChildView: View {
@Binding var count: Int
var body: some View {
Button("Increment") {
count += 1
}
}
}Solution
Step 1: Understand @Binding effect
The ChildView uses @Binding to modify the parent's count state directly.Step 2: Predict UI update
When the button increments count, the parent's Text updates to show the new value.Final Answer:
The count text updates and shows incremented value each tap -> Option CQuick Check:
@Binding syncs changes to parent state [OK]
- Thinking @Binding requires @State in child too
- Assuming UI won't update without explicit refresh
- Confusing @Binding with simple value passing
struct ParentView: View {
@State private var text = "Hello"
var body: some View {
ChildView(text: text)
}
}
struct ChildView: View {
@Binding var text: String
var body: some View {
TextField("Enter", text: $text)
}
}Solution
Step 1: Check how binding is passed
Parent passestextinstead of$text, so ChildView receives a value, not a binding.Step 2: Understand @Binding requirement
ChildView expects a binding, so missing $ causes a type mismatch error.Final Answer:
Parent passes text without $ prefix, so binding is missing -> Option AQuick Check:
Pass binding with $ to match @Binding [OK]
- Omitting $ when passing binding
- Confusing @State and @Binding roles
- Assuming TextField works without binding
struct ParentView: View {
@State private var isOn = false
var body: some View {
ChildView(isOn: $isOn)
.onChange(of: isOn) { newValue in
print("Toggled to \(newValue)")
}
}
}
struct ChildView: View {
@Binding var isOn: Bool
var body: some View {
Toggle("Switch", isOn: $isOn)
}
}Solution
Step 1: Verify @Binding usage
Parent passes binding with $isOn, ChildView uses @Binding and Toggle binds to $isOn correctly.Step 2: Check side effect trigger
Parent uses .onChange(of: isOn) to run code when isOn changes, which works as expected.Final Answer:
This code correctly uses @Binding and triggers side effect on toggle -> Option AQuick Check:
@Binding + onChange in parent = correct pattern [OK]
- Placing onChange inside child instead of parent
- Passing state without $ causing binding errors
- Using Toggle without binding variable
Ready to prove it?
Pass the test and this pattern moves unknown
