I will demonstrate today how to pass data between views in the SwiftUI by using property.

Let’s Create Second View
I have created second view with one property called data, in that we will display data which will be passed from first screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SecondView.swift | |
// PassDataSwiftUI | |
// | |
// Created by Kode on 24/08/22. | |
// | |
import SwiftUI | |
struct SecondView: View { | |
var data = "" | |
var body: some View { | |
Text(data) | |
} | |
} | |
struct SecondView_Previews: PreviewProvider { | |
static var previews: some View { | |
SecondView() | |
} | |
} |
Let’s Create First View
From first view I will pass a sample data to Second View using the property called “data”.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FirstView.swift | |
// PassDataSwiftUI | |
// | |
// Created by Kode on 24/08/22. | |
// | |
import SwiftUI | |
struct FirstView: View { | |
@State private var showSecondView = false | |
var body: some View { | |
Button(action: { | |
self.showSecondView.toggle() | |
}, label: { | |
Text("Open Second View") | |
}) | |
.sheet(isPresented: $showSecondView){ | |
SecondView(data: "Hello 2nd") | |
} | |
} | |
} | |
struct FirstView_Previews: PreviewProvider { | |
static var previews: some View { | |
FirstView() | |
} | |
} |
In Second View you will able to see the text “Hello 2nd” by clicking on “Open Second View” Button on First View.

Conclusion
This is only simple JSON. You can try complex JSON responses!
Let me know if you have any questions, comments, or feedback – via Twitter.
Learn Something New. Share To The World.
Happy Coding 🙂
Share this:
- Click to share on X (Opens in new window) X
- Click to share on Facebook (Opens in new window) Facebook
- Click to share on Reddit (Opens in new window) Reddit
- Click to share on WhatsApp (Opens in new window) WhatsApp
- Click to share on LinkedIn (Opens in new window) LinkedIn
- Click to share on Tumblr (Opens in new window) Tumblr
- Click to share on Telegram (Opens in new window) Telegram
- Click to share on Pocket (Opens in new window) Pocket