diff --git a/NavigationExemple/ContentView.swift b/NavigationExemple/ContentView.swift index 045992c..ad780de 100644 --- a/NavigationExemple/ContentView.swift +++ b/NavigationExemple/ContentView.swift @@ -7,27 +7,42 @@ import SwiftUI +class User: ObservableObject { + @Published var score = 0 +} + +struct ChangeView: View { + @EnvironmentObject var user: User + + var body: some View { + VStack{ + Text("Score : \(user.score)") + Button("Increase"){ + self.user.score += 1 + } + } + } +} + struct ContentView: View { - @State private var isShowingDetailView = false + @ObservedObject var user = User() var body: some View { NavigationView{ VStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: 20){ + Text("Score: \(user.score)") - NavigationLink(destination: Text("Heads"), isActive: $isShowingDetailView){ - EmptyView() - } - - Button("Tap to show view"){ - // Some custom code here - // .. - self.isShowingDetailView = true + NavigationLink(destination: ChangeView()){ + Text("Show detail view") } + }.navigationTitle("Menu principal") } + // Important à ajouter + .environmentObject(user) } }