Ok, so you got me curious on how to create an alert which I assume we will cover later but I was able to find an example here
Here are the steps.
- Add a @State modifier under all your other @State modifiers
@State private var showingAlert = false
- Create the if statement as I described in my previous message
if (creditAmount >= 10) {
// this is where your button code would go.
let imageNameArray = ["apple","cherry","star"]
imageName1 = imageNameArray[Int.random(in: 0...2)]
imageName2 = imageNameArray[Int.random(in: 0...2)]
imageName3 = imageNameArray[Int.random(in: 0...2)]
creditAmount -= 10
} else {
// set the showingAlert which is now tracked via the @State modifier
self.showingAlert = true
}
- After your button code add the .alert modifier like below
Button(
// all of your button code here
).alert(isPresented: $showingAlert) {
Alert(title: Text("You are Out of money"), message: Text("click ok to dismiss"), dismissButton: .default(Text("OK")))
}