presenting a confirm alert after an alert with swiftui not working

921 Views Asked by At

i'm trying to show a confirmation alert after you press the "Confirm" button on the first alert. Idea: You press a button and an alert pops up that asks you to cancel or confirm the action. When you press cancel, the alert gets dismissed and when you press confirm, the action gets performed and a second alert pops up that says something like (title: "Success", message: "the action was successful)" with just a dismiss button. The first alert works just fine and it does the action, but when i add the second alert right after the first alert, the first alert won't show anymore when pressing the button.

code:

Group {
            Button(action: { self.showingAdminAlert = true }, label: {
                                    Text("Als Admin hinzufügen").fontWeight(.bold).font(.system(size: 15)).padding().background(Color.gray).cornerRadius(40).foregroundColor(.white).padding(10).overlay(RoundedRectangle(cornerRadius: 40).stroke(Color.gray, lineWidth: 5))
            }).padding().padding()
            }.alert(isPresented: $showingAdminAlert) {
                Alert(title: Text("Bestätigung erforderlich"), message: Text("Wollen sie \(data.vn) \(data.nn) wirklich die Berechtigung Admin erteilen?"), primaryButton: .cancel(Text("Abbrechen")), secondaryButton: .default(Text("Bestätigen")) {
                    self.AddUserAsAdmin()
                    self.showingAdminAlertConfirmation = true
                    })
            }.alert(isPresented: $showingAdminAlertConfirmation) {
                Alert(title: Text("Erfolgreich"), message: Text("Berechtigung Admin erfolgreich an \(data.vn) \(data.nn) vergeben!"), dismissButton: .default(Text("Zurück")))
            }
1

There are 1 best solutions below

0
On
import SwiftUI

struct ContentView: View
{
    @State var showAlert: Bool = false
    @State var showingAdminAlertConfirmation: Bool = false

    var body: some View {
        
        let Bestätigung =  Alert(title: Text("Bestätigung erforderlich"), message: Text("wirklich die Berechtigung Admin erteilen?"), primaryButton: .cancel(Text("Abbrechen")), secondaryButton: .default(Text("Bestätigen")) {showingAdminAlertConfirmation = true; DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {showAlert = true}  }   )
        let Erfolgreich = Alert(title: Text("Erfolgreich"), message: Text("Berechtigung Admin erfolgreich an vergeben!"), dismissButton: .default(Text("Zurück")) {showAlert = false; showingAdminAlertConfirmation = false}   )

        Button(action: { showAlert = true }, label: {
            Text("Als Admin hinzufügen").fontWeight(.bold).font(.system(size: 15)).padding().background(Color.gray).cornerRadius(40).foregroundColor(.white).padding(10).overlay(RoundedRectangle(cornerRadius: 40).stroke(Color.gray, lineWidth: 5))
        })
        .padding().padding()
        .alert(isPresented: $showAlert) { showingAdminAlertConfirmation ? Erfolgreich : Bestätigung}

        
        
        
    }
}