允许混合状态但不允许点击 NSButton(复选框)

Allow mixed state but not on click with NSButton (checkbox)

我有一个以编程方式设置的复选框 (NSButton)。此复选框将设置为其他 onoffmixed,但当用户单击它时我希望它 仅在 [= 之间循环 11=] 和 off.

目前,当 button.allowsMixedState = true 用户点击复选框时,它将在所有三种状态之间循环。

有没有办法让 button.allowsMixedState = true 但在单击时只有 onoff 之间的复选框循环?

我终于能够通过子类化 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,但是当用户单击它时,复选框只会在 onoff 之间循环。