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
@Statevariable that will trigger the alerts. The variables should be declared immediately below theContentViewstruct declaration and before thebodyvariable:@State private var showTwoButtonsAlert = false @State private var showSimpleAlert = false
- Replace the
Textstruct with aVStackto hold the other structs. We will add the following:VStack{ //The rest...