UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Adding tip percentage alert to WeSplit

Forums > iOS

I'm looking to add an alert to WeSplit that would ask "Are you sure you want to tip 0 %?" but cant figure it out. This isn't an assigned challenge, just something i felt like trying. Any help is appreciated!

3      

You could do this with .onChange which hasn't been covered yet.

Add this to your properties: @State private var showingTipAlert = false

Add this to the Picker, or really anywhere on the Form:

.onChange(of: tipPercentage) { _ in
  if tipPercentage == 0 {
    showingTipAlert = true
  }
}

Add this somewhere to the Form. I usually attach them to the Form itself:

.alert("0% tip?", isPresented: $showingTipAlert) {
  Button("OK") {}
} message: {
  Text("Are you sure you want to tip 0%")
}

or you could do something more basic and instead of doing all that, you could do one of these two options:

attach this modifier to the tipPercentage Picker:

.foregroundColor(tipPercentage == 0 ? .red : .black)

or, instead of that, add this immediately below the Picker section:

if tipPercentage == 0 {
  Text("0% tip, Really?")
    .foregroundColor(.red)
}

Neither of those last two options generate an alert, but they do change the UI to indicate something is happening.

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.