SwiftUI Data Binding with the help of @State and @Binding


Data Binding is an approach which binds the data source (i.e provider) to a consumer and vice-versa , so as to keep them synchronized.

UI Data binding is something which we will be interested to learn along with SwiftUI.


UI Data binding is a design pattern to simplify the development of GUI applications. The name itself gives us an idea about the data will be binding to the View and vice-versa. That is , when data updates the view updates or when the view updates so does the data gets updated.

Just to take as an example, there are many javascript framework which support them example ReactNative or Angular JS Framework.



What would be the traditional approach?


UITextField changes will be identified using the UITextFileDelegate or add Target to get notified when UIControlEventEditingChanged.

Once we receive the notification we update the object.


Seems like it will be too much of code to handle such behavior.


Lately with iOS 13 SwiftUI , we are now able to take advantage of this design pattern, without doing much of a work.


SwiftUI maintains the storage of such property. There are two ways to achieve data binding and both depends on the usage:


@State

- Use @State binding when we want a property to be a single source of truth for a view.


- Consider to use @State ,  when binding to a single view and don't want to pass the binding to its subview.



@Binding

- Binding is used to create a two way connection between the property that stores the data (i.e @State) and view that displays and changes the property state.


- This is usually used when you have a state that manages multiple views. 


- Consider to user @Binding, when binding to a the current view and passing the binding to its subview.


Like in case of below example , when we turn on the "Default Switch" , the Switches of "Yellow Hello, World" and "Purple Hello, World" toggles to on state.


It's because of two way binding, when we toggle the switch in either Yellow or Purple View, all the switches turn off too.


To better understand this, you can refer the StateBindingViewHighlighter





The main ContentView which holds the "highlight" state property managed by SwiftUI.


struct ContentView: View {

    @State private var highlight:Bool = false

}


YellowView and PurpleView which has two way binding with "highlight" property of "ContentView"



struct YellowView: View {

    @Binding var highlightView: Bool

}



struct PurpleView: View {

    @Binding var highlightView: Bool

}



Below diagram helps to understand on how Binding works for YellowView and PurpleView  but would not work with BlueView.



- Both YellowView and PurpleView have two way binding with the ContentView.


- Which is why we can pass projected value of the ContentView , to both of these Views

using the below code snippet


YellowView(highlightView: $highlight)

PurpleView(highlightView: $highlight)


- BlueView does not support because the hightView uses @State property wrapper instead of @Binding property wrapper. 


- Which is why we cannot  pass projected value of the ContentView to BlueView, using  the below code snippet


BlueView(highlightView: highlight)


So when "highlight" changes in contentView  (or Binding property in Yellow or PurpleView) the value would not affect this BlueView.





What exactly is this, @State and @Binding Notation ?


To answer this question it is simply a way to specify a property wrapper. State is a property wrapper which can be applied to a property , by mentioning the wrapper name before the property as an attribute.


To Read more on Property Wrapper , please refer to another blog. 

















Comments

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

Custom Editing Control for UITableViewCell , like iPhone Mail Appliation