Presenting multiple alerts
We have reviewed the process for displaying single alerts in a view. What happens if we have multiple alerts in a single view? When multiple alerts are attached to a view, only one of them works. To create a view with multiple alerts that work, we need to make sure each alert is tied to a different view.
Getting ready
Create a SwiftUI app named PresentingMultipleAlerts
.
How to do it
We will create a VStack
that holds two structs, with a .alert()
modifier attached to each struct. We will use a button click and a Toggle
struct to display alerts. The steps are as follows:
- Create the
@State
variable that will trigger the alerts. The variables should be declared immediately below theContentView
struct declaration and before thebody
variable:@State private var showTwoButtonsAlert = false @State private var showSimpleAlert = false
- Replace the
Text
struct with aVStack
to hold the other structs. We will add the following:VStack{ //The rest...