Skip to content

Instantly share code, notes, and snippets.

@chrapati24
Created September 11, 2024 21:41
Show Gist options
  • Save chrapati24/840effedac44691f8427c1fff9f3703d to your computer and use it in GitHub Desktop.
Save chrapati24/840effedac44691f8427c1fff9f3703d to your computer and use it in GitHub Desktop.
Simple SwiftUI view with a counter (and conditioned Alert)
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