Skip to content

Instantly share code, notes, and snippets.

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