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!


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?")

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


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.