swiftUI:how to get button enabled until all checkbox get checked

swiftUI:how to get button enabled until all checkbox get checked

here is my code:

i want get button(logout) enabled until checkbox(check1~check5) all checked

CheckView.swift:

import SwiftUI

struct CheckView: View {
    @State var isChecked:Bool = false
    var title:String
    var count:Int = 0
    func toggle(){isChecked = !isChecked}
    var body: some View {
        HStack{
            Button(action: toggle) {
                Image(systemName: isChecked ? "checkmark.square" : "square")
            }
            Text(title)
         
        }
    }
}

#if DEBUG
struct CheckView_Previews: PreviewProvider {
    static var previews: some View {
        CheckView(title:"Title")
    }
}
#endif

ExitCheckListData.swift:

import Foundation

let exitCheckListdata = [
     ExitCheckListItem(id:0,title: "check1"),
      ExitCheckListItem(id:1,title: "check2"),
       ExitCheckListItem(id:2,title: "check3"),
        ExitCheckListItem(id:3,title: "check4"),
         ExitCheckListItem(id:4,title: "check5")
]

ContentView.swift:

List(exitCheckListdata){ item in
                                    CheckView(isChecked: item.isChecked, title: item.title)
                 
                                }
                    .font(.title)
                    Button(action: {
                           
                                       }) {
                                           Text("logout")
                                       }
Errors
  • ExitCheckListData.swift

    • ->Use of unresolved identifier 'ExitCheckListItem'

  • ContentView.swift

    • ->Unable to infer complex closure return type; add explicit type to disambiguate

I’ve made this using my interpretation of your idea.
Feel free to change the code to your liking and add your own data model.
This is just something you can use as a sort of template for what you are trying to achieve.

Code Block Swift
struct ContentView: View {
var body: some View {
List {
ForEach(data, id: \.title) { item in
// to have a toggle
Toggle(isOn: $item.isChecked) {
Text(item.title)
}
// to have a checkbox
Button {
item.isChecked.toggle()
} label: {
Label(item.title, systemImage: item.isChecked ? "checkbox.square" : "square")
}
}
Button("Logout") {
// logout
}
.disabled(data.allSatisfy { !$0.isChecked })
}
}
}


swiftUI:how to get button enabled until all checkbox get checked
 
 
Q