Created
April 12, 2020 03:35
-
-
Save imthath-m/5b2996e3ad02586fcc466dfe983baba7 to your computer and use it in GitHub Desktop.
This file contains 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 { | |
private init() { } | |
static let shared = UserSettings() | |
@Published var score = 0 | |
} | |
struct ContentView: View { | |
@ObservedObject var settings: UserSettings = .shared | |
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 { | |
@ObservedObject var settings: UserSettings = .shared | |
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