NSToolbarItem validation in relevant controller

前端 未结 1 1151
太阳男子
太阳男子 2021-02-15 17:06

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

相关标签:
1条回答
  • 2021-02-15 17:27

    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()
        }
    }
    
    0 讨论(0)
提交回复
热议问题