【问题标题】:presenting a confirm alert after an alert with swiftui not working在 swiftui 不工作的警报后显示确认警报
【发布时间】:2020-11-11 09:05:51
【问题描述】:

我正在尝试在您按下第一个警报上的“确认”按钮后显示确认警报。 想法:您按下一个按钮,会弹出一个警报,要求您取消或确认该操作。当您按下取消时,警报被解除,当您按下确认时,操作被执行并弹出第二个警报,上面写着类似(标题:“成功”,消息:“操作成功)”,只有一个解除按钮. 第一个警报工作正常并且它执行操作,但是当我在第一个警报之后添加第二个警报时,按下按钮时第一个警报将不再显示。

代码:

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")))
            }

【问题讨论】:

  • 每个视图只能有一个警报。您可以使用类似于stackoverflow.com/questions/58737767/… 的方法组合警报。一旦第一个警报被解除,您可能还需要延迟显示第二个警报。

标签: swiftui swiftui-alert


【解决方案1】:
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}

        
        
        
    }
}

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-04-30
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多