-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"))) } 
1
  • 1
    You can only have one alert on each view. You could combine the alerts using a method similar to this stackoverflow.com/questions/58737767/…. You may also need to delay showing the second alert once the first alert has been dismissed. Commented Nov 11, 2020 at 9:14

1 Answer 1

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} } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.