Skip to content

Instantly share code, notes, and snippets.

@imthath-m
Created April 12, 2020 03:35
Show Gist options
  • Save imthath-m/5b2996e3ad02586fcc466dfe983baba7 to your computer and use it in GitHub Desktop.
Save imthath-m/5b2996e3ad02586fcc466dfe983baba7 to your computer and use it in GitHub Desktop.
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