What is the @Binding property wrapper?
What is the @Binding property wrapper? êŽë š
Updated for Xcode 15
@Binding
lets us declare that one value actually comes from elsewhere, and should be shared in both places. This is not the same as @ObservedObject
or @EnvironmentObject
, both of which are designed for reference types to be shared across potentially many views.
For example, we might have a ContentView
with an @State
property storing whether a child view is being presented or not, like this:
struct ContentView: View {
@State private var showingAddUser = false
var body: some View {
VStack {
// your code here
}
.sheet(isPresented: $showingAddUser) {
// show the add user view
}
}
}
That uses showingAddUser
for the isPresented
parameter of our sheet, which means when that Boolean becomes true the add user view will be shown. However, how can we allow the add user view to dismiss itself if it needs to â if the user taps a Done button, for example?
What we want to happen is for the add user view to set showingAddUser
back to false, which will cause ContentView
to hide it. This is exactly what @Binding
is for: it lets us create a property in the add user view that says âthis value will be provided from elsewhere, and will be shared between us and that other place.â
So, we might create an add user view like this:
struct AddView: View {
@Binding var isPresented: Bool
var body: some View {
Button("Dismiss") {
isPresented = false
}
}
}
That property literally means âI have a Boolean value called isPresented
, but itâs being stored elsewhere.â So, when we create that AddView
to replace the // show the add user view
comment from earlier, weâd need to provide the value so it can be manipulated:
.sheet(isPresented: $showingAddUser) {
AddView(isPresented: $showingAddUser)
}
This allows both ContentView
and AddView
to share the same Boolean value â when it changes in one place it also changes in the other.