我有一个以编程方式设置的复选框(NSButton
)。此复选框将设置为其他on
,off
或mixed
,但当用户点击它时,我希望仅在on
之间循环和off
。
目前,当button.allowsMixedState = true
用户点击复选框时,它会在所有三种状态之间循环。
有没有办法获得button.allowsMixedState = true
但只有on
和off
之间的复选框周期才会被点击?
答案 0 :(得分:2)
通过继承NSButtonCell
而不是NSButton
并覆盖其nextState
属性,我终于能够正常工作:
(此示例使用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
}
}
}
这意味着我可以将复选框的状态设置为mixed
,但是当用户点击它时,复选框只会在on
或off
之间循环。