简体   繁体   English

尝试将NSPopupButton的`enabled`属性绑定到复选框的状态

[英]Trying to bind a NSPopupButton's ```enabled``` property to a checkbox's state

Using cocoa bindings, I want that a popup button is enabled or disabled based on whether the checkbox is selected or not. 使用可可粉绑定,我希望根据是否选中复选框来启用或禁用弹出按钮。

I am programatically adding checkboxes and popups to a stack view like this: 我以编程方式将复选框和弹出窗口添加到这样的堆栈视图中:

  private func addCheckBoxes() {
        for period in String.BuildTimePeriod.allCases {
            let checkbox = NSButton(checkboxWithTitle: period.rawValue.capitalized, target: self, action: nil)
            checkbox.bind(.value, to: NSUserDefaultsController.shared, withKeyPath: "values.\(period.defaultsBoolKey.rawValue)", options: [NSBindingOption.continuouslyUpdatesValue: true])
            stackView.addArrangedSubview(checkbox)
            addPopUpToCheckbox(checkbox, period: period)
        }
    }

    private func addPopUpToCheckbox(_ checkbox: NSButton, period: String.BuildTimePeriod) {
        let popUp = NSPopUpButton(frame: checkbox.frame, pullsDown: false)
        let timeBlocks = String.TimeBlock.allCases.map() { $0.rawValue.capitalized }
        popUp.addItems(withTitles: timeBlocks)
        popUp.bind(.selectedValue, to: NSUserDefaultsController.shared, withKeyPath: "values.\(period.rawValue)", options: [NSBindingOption.continuouslyUpdatesValue: true])
        popUp.bind(.enabled, to: checkbox, withKeyPath: "state", options: [NSBindingOption.continuouslyUpdatesValue : true])
        stackView.addArrangedSubview(popUp)
    }

The binding works on initial load, of the controller, however, the popUps enabled state do not change dynamically. 绑定在控制器的初始负载上起作用,但是popups启用状态不会动态更改。 Wondering if anyone can provide any guidance. 想知道是否有人可以提供任何指导。

感谢@Willeke,我意识到这是因为我只是在引用"state" ,而不是按钮所需要的"cell.state"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM