[Swiftui] 兩個alert

有時會有一些情況,在判斷之後,需要顯示出不一樣的 alert
本來想說寫兩個 .Alert 就可以
不過最後會只有最後一個 Alert 發揮作用
所以要這樣改

enum ActiveAlert {
    case IncorrectPWD, ChangePWD, None
}

struct Change_Password: View {
    @State private var showAlert = false
    @State private var activeAlert: ActiveAlert = .None

在需要的地方把設定值設定下去
    self.activeAlert = .IncorrectPWD
    self.showAlert = true

最後再要產生 alert 的地方這樣寫
.alert(isPresented: $showAlert) {
                        print("4 activeAlert = \(self.activeAlert)")
                        switch activeAlert {
                            case .IncorrectPWD:
                                return Alert(title: Text("Incorrect Input Password"))
                            case .ChangePWD:
                                return Alert(title: Text("Password Changed"))
                            case .None:
                                return Alert(title: Text("None....."))
                        }

這樣就會維持一個 alert 可是可以有多個顯示訊息

留言

這個網誌中的熱門文章

Ubuntu 64bit 32bit 的問題

[RDKM] Set up Env. in Windows10 WSL