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. Wondering if anyone can provide any guidance.
感谢@Willeke,我意识到这是因为我只是在引用"state"
,而不是按钮所需要的"cell.state"
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.