使用可可绑定,我希望根据是否选中此复选框来启用或禁用弹出按钮。
我正在以编程方式将复选框和弹出窗口添加到这样的堆栈视图中:
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)
}
绑定在控制器的初始负载上起作用,但是popups启用状态不会动态更改。想知道是否有人可以提供任何指导。
答案 0 :(得分:0)
感谢@Willeke,我意识到这是因为我只是在引用"state"
,而不是按钮所需要的"cell.state"
。