Alert problem in SwiftUI

Hello all,
When I use Alert in SwiftUI, I realize that I can only show number of Alert less than or equal number of View. Example: I have a Button to show a random Alert: True Alert and False Alert. But only underneath Alert (False Alert) is shown and the above Alert (True Alert) is ignored:

struct ContentView: View {

@State var showTrueAlert = false
@State var showFalseAlert = false

var body: some View {
    Button(action: {
        let isTrue = Bool.random()
        if isTrue {
            self.showTrueAlert = true
            print("True Alert")
        } else {
            self.showFalseAlert = true
            print("False Alert")
        }
    }) {
        Text("Random Alert")
            .font(.largeTitle)
    }
    .alert(isPresented: $showTrueAlert) {
        Alert(title: Text("True"))
    }
    .alert(isPresented: $showFalseAlert) {
        Alert(title: Text("False"))
    }
}

}

Is any one meet this problem? And can anyone can solve this

I'm just guessing:

For some modifiers the order determines which one applies. For example, foregroundColor(): the first/nearest one applies, later ones are ignored.

It could very well be the case that for the .alert() modifier, the last one gets "registered" to the view, overriding earlier ones.

Show whichever with one modifier?

.alert(isPresented: $showAlert) {
    Bool.random() ? Alert(title: Text("True")) :
    Alert(title: Text("False"))
}

SwiftUI should give warning in debug mode when modifiers do not make sense. Seem you can add modifier to whatever, even when it doesn't make sense...

Terms of Service

Privacy Policy

Cookie Policy