Cannot convert value of type 'AlertViewPOP' to closure result type 'Alert'

38 Views Asked by At
.alert(isPresented: self.$addANoteAlert) {
    AlertViewPOP(
        isPresented: self.$addANoteAlert,
        title: "Alert Title",
        message: "This is a message.",
        actionTitle: "OK",
        actionHandler: {
            // Handle the OK action
            print("OK tapped")
        }
    )
}

When I am trying to add this in view, I get this error:

Cannot convert value of type 'AlertViewPOP' to closure result type 'Alert'

1

There are 1 best solutions below

0
On BEST ANSWER

alert(isPresented:content:) modifier expects a closure that returns an Alert, not a custom view (AlertViewPOP).

if you want to use your custom alert try using sheet or you can render it in your view by using a alert flag State var something like this :

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

    var body: some View {
        Button("Show Alert") {
            alertFlag = true
        }

        if alertFlag {
            AlertViewPOP(
                isPresented: $alertFlag,
                title: "Alert Title",
                message: "This is a message.",
                actionTitle: "OK",
                actionHandler: {
                    print("OK tapped")
                }
            )
        }
    }
}