如何实现validateToolbarItem(_ :)? [英] How to implement validateToolbarItem(_:)?

查看:97
本文介绍了如何实现validateToolbarItem(_ :)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟随此问题文档示例。我试图实现一段代码,以在macOS工具栏中启用和禁用两个按钮(撤消和重做)。

Following this question and the documentation example. I tried to implement a piece of code that enable and disable two buttons (Undo and Redo) in a macOS Toolbar.

override func validateToolbarItem(_ toolbarItem: NSToolbarItem) -> Bool {

    var enable = false

    if toolbarItem.itemIdentifier.isEqual("undoButton") {
        enable = (mainTextField.undoManager?.canUndo)!
    }
    else if toolbarItem.itemIdentifier.isEqual("redoButton") {
        enable = (mainTextField.undoManager?.canRedo)!
    }

    return enable
}

不幸的是似乎代码没有作用。我缺少什么?

Unfortunately it seems that the code has not effect. What am I missing?

推荐答案

    enum toolItems:Int {
        case undo = 0
        case redo = 1
    }

    // creating an array at the beginning (AppleDelegate, windowDidLoad, ...) //
    func makeToolbar() {
        toolbarItemState.insert("1", at: toolItems.undo.rawValue) // 0
        toolbarItemState.insert("0", at: toolItems.redo.rawValue) // 1
    }

    override func validateToolbarItem(_ toolbarItem:NSToolbarItem) -> Bool {
        var enable:Bool = false
        if ((toolbarItemState[toolbarItem.tag] as AnyObject).integerValue == 1) {
            enable = true
        }
        return enable
    }

    func editToolItem(index:Int,state:String) -> Void {
        toolbarItemState.replaceObject(at: index, with: state)
    }



<当应用程序启动时,创建一个toolbarItemState数组。例如,如果要将撤消工具栏项目的状态更改为 on,则

When the application launches, create an array of toolbarItemState. If you want to change the state of the undo toolbar item to 'on,' for example,

editToolItem(index: toolItems.savePict.undo, state: "1")

。现在,撤消工具栏项为1。如果将状态设置为 0,则该按钮将被禁用。

. Now, the undo toolbar item is one. If you set the state to "0," the button will be disabled.

这篇关于如何实现validateToolbarItem(_ :)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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