🔸binding
SwiftUI ⟩ view ⟩ state ⟩ binding
A binding connects a property to a source of truth stored elsewhere.
Add @Binding to create a two-way connection between a property that stores data, and a view that displays and changes the data.
struct ContainerView: View {
// ⭐️ container's view state (source of truth)
@State var isOn = false
var body: some View {
VStack {
// ⭐️ 在 subview (Toggle) 中使用 binding ($isOn)
Toggle("Press Me", isOn: $isOn)
}
}
}
Last updated
Was this helpful?