Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 962 Bytes

File metadata and controls

28 lines (19 loc) · 962 Bytes

Use Cocoa Bindings to conditionally enable a button if one or more checkbox are checked.

Take away is to use KVO Registering Dependent Keys.

  1. Use a dynamic variable with computed properties:
    dynamic var atLeastOneSelectedCheckbox: Bool {
        return [checkbox1, checkbox2, checkbox3].contains(where: { $0.state == NSOnState })
    }
  1. Then make sure it gets computed everytime the state of a checkbox changes with:
    dynamic class func keyPathsForValuesAffectingAtLeastOneSelectedCheckbox() -> Set<String> {
        return ["checkbox1.cell.state",
                "checkbox2.cell.state",
                "checkbox3.cell.state"]
    }
  1. And set the binding of the button to self. atLeastOneSelectedCheckbox