Skip to content

Enable Button If At Least One Checkbox Is Selected with Cocoa Bindings

Notifications You must be signed in to change notification settings

ftiff/EnableButtonIfAtLeastOneCheckboxIsSelected

Repository files navigation

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

About

Enable Button If At Least One Checkbox Is Selected with Cocoa Bindings

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages