-1

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")))
            }
Merdis
  • 37
  • 4
  • 1
    You can only have one alert on each view. You could combine the alerts using a method similar to this https://stackoverflow.com/questions/58737767/how-to-show-different-alerts-based-on-a-condition-after-clicking-a-button-in-swi/58738292#58738292. You may also need to delay showing the second alert once the first alert has been dismissed. – Andrew Nov 11 '20 at 09:14

1 Answers1

2
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}

        
        
        
    }
}
ios coder
  • 1
  • 4
  • 31
  • 91