How to pass data from one view to another in SwiftUI?

import SwiftUI

struct ContentView: View {
     
    @State private var valueToPass : Int = 0

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    self.valueToPass += 1
                }) {
                    Text("Increase value \(self.valueToPass)")
                }
                 
                NavigationLink(destination: firstLevelView(valueToGet: $valueToPass)) {
                    Text("Show Detail View")
                }
            }
        }
    }
}
 
struct firstLevelView: View {

    @Binding var valueToGet: Int

    var body: some View {
        NavigationView {
            VStack {
                Text("Show value \(valueToGet)")
            }
        }
    }
}
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s