SwiftUI: alert messages

A common way to give feedback to users, but also to help you debug your applications sometimes, is to use alerts.

SwiftUI provides the .alert() modifier that we can use to show an alert based on some condition.

Let’s start from this ex…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

A common way to give feedback to users, but also to help you debug your applications sometimes, is to use alerts.

SwiftUI provides the .alert() modifier that we can use to show an alert based on some condition.

Let’s start from this example where we have a Button with a counter:

import SwiftUI

struct ContentView: View {
    @State var count = 0
    
    var body: some View {
        Button("Count: \(count)") {
            self.count += 1
        }
        .font(.title)
    }
}

when count reaches 10 I want to show an alert message.

How do we do that?

I can add a showAlert boolean property to the ContentView and edit the content of the Button tap action:

struct ContentView: View {
    @State var count = 0
    @State var showAlert = false
    
    var body: some View {
        Button("Count: \(count)") {
            self.count += 1
            if self.count == 10 {
                showAlert = true
                self.count = 0
            }
        }
        .font(.title)
    }
}

When we reach 10, we set showAlert to true and we set the count to 0.

Then I add the .alert() modifier to the Button view:

.alert(isPresented: $showAlert) {
    Alert(title: Text("Great!"), message: Text("You reached 10"))
}

This alert is shown only when the showAlert property is true. When we dismiss the alert, the showAlert property is automatically set to false.

Here’s the full code:

struct ContentView: View {
    @State var count = 0
    @State var showAlert = false
    
    var body: some View {
        Button("Count: \(count)") {
            self.count += 1
            if self.count == 10 {
                showAlert = true
                self.count = 0
            }
        }
        .font(.title)
        .alert(isPresented: $showAlert) {
            Alert(title: Text("Great!"), message: Text("You reached 10"))
        }
    }
}

Try running the app. The counter starts at 0. Click the button and the counter will increase:

Until you reach 10:

Then the counter will start again from 0:


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-09-19T05:00:00+00:00) SwiftUI: alert messages. Retrieved from https://www.scien.cx/2021/09/19/swiftui-alert-messages/

MLA
" » SwiftUI: alert messages." flaviocopes.com | Sciencx - Sunday September 19, 2021, https://www.scien.cx/2021/09/19/swiftui-alert-messages/
HARVARD
flaviocopes.com | Sciencx Sunday September 19, 2021 » SwiftUI: alert messages., viewed ,<https://www.scien.cx/2021/09/19/swiftui-alert-messages/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI: alert messages. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/19/swiftui-alert-messages/
CHICAGO
" » SwiftUI: alert messages." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/09/19/swiftui-alert-messages/
IEEE
" » SwiftUI: alert messages." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/19/swiftui-alert-messages/. [Accessed: ]
rf:citation
» SwiftUI: alert messages | flaviocopes.com | Sciencx | https://www.scien.cx/2021/09/19/swiftui-alert-messages/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.