Presenting alerts
A common way of letting the user know that something important happened is to present an alert with a message and an OK button. In this recipe, we will create a simple alert that gets displayed when a button is pressed.
Getting ready
Create a SwiftUI application named PresentingAlerts
.
How to do it
We display alerts by creating an Alert
and setting up the condition for when it should be displayed using a @State
variable. The alert will contain a title, text, and dismiss button.
The process is as follows:
- Create a
@State
variable whose value determines if the alert is shown or not:@State private var showSubmitAlert = false;
- Replace the
Text
struct inContentView
with aButton
with the label"Submit"
:Button(action: { //button action }){ Text("Submit") &...