Created
April 12, 2020 03:14
-
-
Save imthath-m/96bd3c279f0488a32f061c31327563e3 to your computer and use it in GitHub Desktop.
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
class UserSettings: ObservableObject { | |
@Published var score = 0 | |
} | |
struct ContentView: View { | |
@EnvironmentObject var settings: UserSettings | |
var body: some View { | |
NavigationView { | |
VStack { | |
// A button that writes to the environment settings | |
Button(action: { | |
self.settings.score += 1 | |
}) { | |
Text("Increase Score") | |
} | |
NavigationLink(destination: DetailView()) { | |
Text("Show Detail View") | |
} | |
} | |
} | |
} | |
} | |
struct DetailView: View { | |
@EnvironmentObject var settings: UserSettings | |
var body: some View { | |
// A text view that reads from the environment settings | |
Text("Score: \(settings.score)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment