I'm trying to create a simple labeled checkbox in SwiftUI that toggles an isChecked when the button is pressed. It's very similar to the example in Apple's Binding documentation:
import SwiftUI
struct LabeledCheckbox: View {
var labelText: String
@Binding var isChecked: Bool
var checkBox: Image {
Image(systemName: isChecked ? "checkmark.circle" : "circle")
}
var body: some View {
Button(action: {
self.isChecked.toggle()
}) {
HStack {
checkBox
Text(labelText)
}
}
}
}
struct LabeledCheckbox_Previews: PreviewProvider {
static var previews: some View {
LabeledCheckbox(labelText: "Checkbox", isChecked: .constant(false))
}
}
Unfortunately, when I interact with this button in "Live Preview" mode, the button action doesn't seem to actually toggle isChecked. You can view that here. I put a print(isChecked ? "checked" : "unchecked") debug statement in the action closure and observed in the console output the that isChecked was never being toggled.
I've also attempted to get LabeledCheckboxto be toggable by passing in a state variable in LabeledCheckbox_Previews. The button is still not toggable.
struct LabeledCheckbox_Previews: PreviewProvider {
@State private static var isChecked = false
static var previews: some View {
LabeledCheckbox(labelText: "Checkbox", isChecked: $isChecked)
}
}
What am I doing wrong? Any help is appreciated.
isCheckedis a.constant(false), so can't be changed.isCheckedand and inLabeledCheckbox_Previewsand pass it into the view, ala@State private static var isChecked = false. That didn't work either.