WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.