I have an NSToolbarItem with an NSButton as its view and an NSMenuItem in the main menu. Both have the same action, which is sent to the first responder, not to a particular tar
I wrote the following code in my NSToolbarItem subclass for buttons. With this toolbarItem subclass, you can use normal validateUserInterfaceItem()
or validateToolbarItem()
to validate toolbar items that contain an NSControl.
override func validate() {
// validate content view
if
let control = self.view as? NSControl,
let action = self.action,
let validator = NSApp.target(forAction: action, to: self.target, from: self) as AnyObject?
{
switch validator {
case let validator as NSUserInterfaceValidations:
control.isEnabled = validator.validateUserInterfaceItem(self)
default:
control.isEnabled = validator.validateToolbarItem(self)
}
} else {
super.validate()
}
}