Swift and SwiftUI tutorials for Swift Developers

What is @Binding in SwiftUI

Binding is one of several property wrappers that SwiftUI presents to us to control the flow of data in the application. Binding provides us with a reference as an access to a value type.

Binding is used when you want to bind a value or object that belongs to a different view.

We use @Binding to propagate the value of a property between different views. For example, passing a variable from view A to view B. This allows for interconnection of states thus giving the possibility of data always being up to date between all views that use that property.

To use @Binding you first define a property with the @Binding wrapper and then pass the binding to another view as an argument with the $ symbol. The other view can use the binding to read and write data from the original view.

The @Binding keyword in SwiftUI is a property wrapper, meaning it wraps a property with some additional functionality. In the case of @Binding, you are synthesizing a value of type Binding, where Value is the data type you are binding.

A binding in SwiftUI is a connection between a value and a view that displays and modifies it. You can create your own bindings using the @Binding property wrapper and pass bindings to views that require them. As the name implies, a connection between two things. In SwiftUI, a binding sits between a property that stores data and a view that displays and modifies that data.

When you use @Binding, you’re basically creating a live connection between the data in the parent view and the data in the child view. This means that any changes you make to the data within the child view will be instantly reflected in the parent view and vice versa. It’s like having a real-time sync feature for your data!

@Binding var text: String

The property that the @Binding directive is assigned to will serve as a sort of proxy for the View that needs to access the “source of truth,” which, in most cases, is a property of the parent. That property would be assigned a @State directive indicating that the value it contains is bound to the state of the View.

The goal of this is to bind interactive elements between a hierarchy of views and persistent states to enable functionality. Think of this like the Storyboard connections you had to make manually, but now it actually makes sense.

A view in SwiftUI can contain multiple child views. You might want to communicate with those child views, or you might want a child view to change the value of the parent view.

Using @Binding, you can pass the value of the @State variable from the parent view to the child view, and if the child view modifies, mutates, or changes the value of @State, the parent view will automatically get the updated value and re-render the view.

This means that you can update the parent view from the child view by simply changing the value of the parent view’s @State variable in the child view using @Binding.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

What is @State in SwiftUI

Next Article

SwiftUI Animations: Explicit and Implicit Animations

Related Posts