ContentView.swift
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)")
}
}
UserSettings.swift
import Foundation
class UserSettings: ObservableObject {
@Published var score = 0
}
App.swift
import SwiftUI
@main
struct aPTEApp: App {
var settings = UserSettings()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(settings)
}
}
}