Button with Label and Alert

Viewed 222

I have this button

Button { }
label: {
    Image(systemName: "info.circle")
}

and want to couple it with this alert

.alert(isPresented: $showAlert) {
    Alert(title: Text("Impressum"), message: Text("?"), dismissButton: .default(Text("Okay")))

Using SwiftUI... It's not working.

1 Answers

You need to actually toggle the showAlert state.

Code:

struct ContentView: View {
    @State private var showAlert = false

    var body: some View {
        Button {
            showAlert.toggle()
        } label: {
            Image(systemName: "info.circle")
        }
        .alert(isPresented: $showAlert) {
            Alert(title: Text("Impressum"), message: Text("?"), dismissButton: .default(Text("Okay")))
        }
    }
}

Result:

Result

Related