相关控制器中的NSToolbarItem验证 [英] NSToolbarItem validation in relevant controller

查看:107
本文介绍了相关控制器中的NSToolbarItem验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有NSButton的NSToolbarItem视图,主菜单中有一个NSMenuItem.两者具有相同的动作,该动作被发送到第一响应者,而不是特定的目标.该方法最终在窗口内容视图的视图层次结构中的NSSplitViewController的子类中实现.我想验证两个项目,但是让特定的拆分视图控制器负责验证,因为它依赖于该控制器本地的某些条件.

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 target. That method is ultimately implemented in a subclass of NSSplitViewController, somewhere in the view hierarchy of the window’s content view. I want to validate both items, but have that specific split-view controller take care of the validation, because it relies on some conditions local to that controller.

我在该拆分视图控制器中覆盖了validateToolbarItem(_:)validateMenuItem(_:).对于菜单项,它按预期工作.调用该方法,然后进行验证.但是,永远不会调用validateToolbarItem(_:).

I overrode validateToolbarItem(_:) and validateMenuItem(_:) in that split-view controller. For the menu item, this is working as expected. The method is called and the validation happens. validateToolbarItem(_:) is never called, however.

根据 Apple的文档 ,NSToolbar不会将validateToolbarItem(_:)发送到基于视图的工具栏项目.为了测试这一点,我用图像工具栏项替换了工具栏项,并且该工具栏按预期工作.

According to Apple’s documentation, NSToolbar does not send validateToolbarItem(_:) to view-based toolbar items. To test this, I have substituted the toolbar item with an image toolbar item and there it works as expected.

基于此,我遇到了几种解决方案,但是它们并不是我想要的.

Based on this, I have come across several solutions, but they aren’t quite what I want.

  • 子类化NSToolbarItem并覆盖validate().但是,没有关于如何最终使控制器的validateToolbarItem(_:)呼叫的指导.

  • Subclass NSToolbarItem and override validate(). However, no guidance is given as to how I end up getting the controller’s validateToolbarItem(_:) to call.

子类化NSToolbar并覆盖validateVisibleToolbarItems(),然后将消息发送给第一响应者.在这里,我遇到了一个问题,我无法向拆分视图控制器发送消息,因为它不在工具栏的响应程序链之内.

Subclass NSToolbar and override validateVisibleToolbarItems(), then send messages to the first responder. Here I am running into the problem, that I cannot send a message to the split-view controller, because it is outside of the toolbar’s responder chain.

NSToolbar的子类如上所述,但是在响应者链中的控制器(例如NSWindowController)中实现validateToolbarItem(_:).这会起作用,但随后我必须添加其他代码来处理菜单项不必要的内容.

Subclass NSToolbar as above, but implement validateToolbarItem(_:) in a controller that is within the responder chain, such as the NSWindowController. This would work, but then I have to add additional code to handle what is not necessary for the menu item.

是否有一个优雅的解决方案可以像图像工具栏项和菜单项一样正常工作?

Is there an elegant solution for this that works as well like it does for the image toolbar item and the menu item?

推荐答案

我在NSToolbarItem子类中为按钮编写了以下代码.有了这个toolbarItem子类,您可以使用普通的validateUserInterfaceItem()validateToolbarItem()来验证包含NSControl的工具栏项目.

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()
    }
}

这篇关于相关控制器中的NSToolbarItem验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆