Created
September 11, 2024 21:41
-
-
Save chrapati24/840effedac44691f8427c1fff9f3703d to your computer and use it in GitHub Desktop.
Simple SwiftUI view with a counter (and conditioned Alert)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct CounterView: View { | |
@State private var counter = 0 | |
@State private var showAlert = false | |
var body: some View { | |
VStack { | |
Text("\(counter)") | |
Button("+") { | |
counter += 1 | |
if(self.counter == 10) { | |
showAlert = true | |
counter = 0 | |
} | |
} | |
.alert(isPresented: $showAlert) { | |
alertCounter() | |
} | |
Button("-") { | |
counter -= 1 | |
if(self.counter == -10) { | |
showAlert = true | |
counter = 0 | |
} | |
} | |
.alert(isPresented: $showAlert) { | |
alertCounter() | |
} | |
Button("Reset") { | |
counter = 0 | |
} | |
} | |
} | |
func alertCounter() -> Alert { | |
return Alert(title: Text("Stop"), message: Text("Le compteur va revenir à 0.")) | |
} | |
} | |
#Preview { | |
CounterView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment