From 6ab7d61067eb3f3d4ee63ec48d6e4d991e5a3c10 Mon Sep 17 00:00:00 2001 From: "art.dambrine" Date: Tue, 6 Apr 2021 10:33:29 +0200 Subject: [PATCH] =?UTF-8?q?les=20pre=CC=81mices=20d'une=20navbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NavigationExemple/ContentView.swift | 45 +++++++++++------------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/NavigationExemple/ContentView.swift b/NavigationExemple/ContentView.swift index ad780de..93ceb5b 100644 --- a/NavigationExemple/ContentView.swift +++ b/NavigationExemple/ContentView.swift @@ -7,42 +7,31 @@ 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 { - @ObservedObject var user = User() + @State private var score = 0 var body: some View { NavigationView{ - VStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: 20){ - Text("Score: \(user.score)") - - NavigationLink(destination: ChangeView()){ - Text("Show detail view") - } - - - }.navigationTitle("Menu principal") + Text("Score: \(score)") + .navigationTitle("Menu principal") + .navigationBarItems( + + trailing: + HStack{ + Button("Add"){ + self.score += 1 + } + + Button("Sub"){ + self.score -= 1 + } + } + + ) } - // Important à ajouter - .environmentObject(user) } }