swiftui

I need to fix SwiftUI


import SwiftUI

struct ContentView: View {
    
    @State var isError = false
    
    var body: some View {
        Button {
            isError = true
        } label: {
            Text("CHMO")
                .background(Color.orange)
                .cornerRadius(10)
        }
        .confirmationDialog("are you sure", isPresented: $isError) {
            Alert(title: Text("Gooo"), message: Text("No More"), primaryButton: .default(Text("Yes")), secondaryButton: .cancel(Text("No")))
        }
        
        VStack {
            Image(systemName: "globe")
                .imageScale(.medium)
                .foregroundStyle(.brown)
            Text("Hello, world!")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

in this part of the code it gives an error:

.confirmationDialog("are you sure", isPresented: $isError) {
            Alert(title: Text("Gooo"), message: Text("No More"), primaryButton: .default(Text("Yes")), secondaryButton: .cancel(Text("No")))
        }
Static method 'buildExpression' requires that 'Alert' conform to 'View'

I need to understand how to work with .confirmationdialog Fix this problem PLS


Solution

  • .confirmationDialog is similar to an Alert. You cannot put an Alert in the actions closure

    Replace it with

    .confirmationDialog("are you sure", isPresented: $isError, titleVisibility: .visible) {
        Button("Yes") { doSomethingOnYes() }
    } message: {
        Text("No More")
    }
    

    The Cancel button is for free