SwiftUI — Data transfer between Structs

lyvennitha sasikumar
2 min readJul 17, 2021

The way of transferring Data between the views make programers sensible.
Usually we transfer data between views using segues, push viewcontroller, presenting viewcontroller and Protocol delegates while using storyboards and xibs. But in SwiftUI it is bit different. I’ve explain here how to transfer datas between struct of views.

passing value between view

Variable Declaration:

Declaring variable is handling here in two ways. One is the variable which sends the data from one struct. Another which accepts the data from another struct mainly.

struct ContentView : View {
@State public var InitStr: String = "Hello World from View!" //variable //declaration

var body: some View{
Text("Hello World")
}
}

Receiving Variable:

struct WeatherView: View{@Binding var Str: String = "Hello Text" var body: some View{
Text(Str)
}
}

Ever wonder about @ Binding :

Binding is a variable type where it declares in one place and its value can get from anywhere and the value for binding variable is assigned as address of another variable of its own source. (like a pointer)

How to pass value:

Now we will see how to pass value to binding variable.

struct ContentView : View {
@State public var InitStr: String = "Hello World from View!" //variable //declaration

var body: some View{
NavigationLink(destination: WeatherView($InitStr)){//passing initstr //to Str of weatherView
Text("Hello World")
}
}
}

Now Tapping on Text in screen, navigates to WeatherView where in that view, the variable value from first struct is displayed.

Happy Coding🐶!

--

--

lyvennitha sasikumar

Passionate iOS developer chasing my dreams toward success