I have a checkbox (NSButton
) that I set programmatically. This checkbox will be set to other on
, off
or mixed
, but when the user clicks it I want it to only cycle between on
and off
.
Currently, when button.allowsMixedState = true
when the user clicks on the checkbox it will cycle between all three states.
Is there a way to have button.allowsMixedState = true
but only have the checkbox cycle between on
and off
when it's clicked?
I was able to finally get this working, by subclassing the NSButtonCell
rather than the NSButton
and overriding its nextState
property:
(This example was run with Swift 4)
// 0 is .off, 1 is .on and -1 is .mixed.
// We override nextState so that it can never be -1.
class MyButtonCell: NSButtonCell {
override var nextState: Int {
get {
return self.state == .on ? 0 : 1
}
}
}
This means I can set the checkbox's state to mixed
, but when the user clicks it the checkbox will only cycle between on
or off
.