SwiftUI State VS Binding
SwiftUI State VS Binding
@State @Binding in SwiftUI with example and explanation
@Binding: Used to create a two-way connection between a parent view and its child view. It allows the child view to read and modify a value owned by the parent view.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct ParentView: View {
@State private var toggleState = false
var body: some View {
VStack {
ChildView(isToggled: $toggleState)
Text("Toggle State: \(toggleState.description)")
}
}
}
struct ChildView: View {
@Binding var isToggled: Bool
var body: some body {
Toggle("Toggle", isOn: $isToggled)
}
}
This post is licensed under CC BY 4.0 by the author.