Complete the code to declare a binding variable in the child view.
struct ChildView: View {
@[1] var isOn: Bool
var body: some View {
Toggle("Switch", isOn: $isOn)
}
}Use @Binding to create a two-way connection from the parent to the child view.
Complete the code to pass a binding from the parent to the child view.
struct ParentView: View {
@State private var isOn = false
var body: some View {
ChildView(isOn: [1])
}
}Use $ to pass a binding of the state variable to the child.
Fix the error in the child view declaration to correctly use @Binding.
struct ChildView: View {
@Binding var isOn: Bool
var body: some View {
Toggle("Switch", isOn: $[1])
}
}The toggle needs a binding, so the variable must be declared with @Binding and used with $isOn.
Fill both blanks to create a child view that toggles a binding boolean and shows text based on it.
struct ChildView: View {
@[1] var isOn: Bool
var body: some View {
VStack {
Toggle("Switch", isOn: $isOn)
Text(isOn ? [2] : "Off")
}
}
}The child uses @Binding for the boolean and shows "On" text when true.
Fill all three blanks to create a parent view with a state boolean, passing it as a binding to a child view.
struct ParentView: View {
@[1] private var isOn = false
var body: some View {
[2](isOn: [3])
}
}The parent owns the state with @State, uses ChildView as child, and passes $isOn as binding.