如何启用/禁用NSToolbarItem [英] how to enable/disable NSToolbarItem

查看:122
本文介绍了如何启用/禁用NSToolbarItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目需要禁用/启用某些NSToolbarItem,具体取决于不同的选项.我检查后发现没有参数.

I have a project that needs to disable/enable some NSToolbarItems depends on different options. I checked and found no parameter for this.

是否可以启用/禁用给定的NSToolbarItem?

Is there a way to enable/disable a given NSToolbarItem?

推荐答案

在窗口,视图或文档控制器中实施NSToolbarItemValidation协议.该文档提供了以下示例代码:

Implement NSToolbarItemValidation Protocol in your window, view or document controller. The documentation gives the following sample code:

-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem {

    BOOL enable = NO;
    if ([[toolbarItem itemIdentifier] isEqual:SaveDocToolbarItemIdentifier]) {

        // We will return YES (enable the save item)
        // only when the document is dirty and needs saving
        enable = [self isDocumentEdited];

    } else if ([[toolbarItem itemIdentifier] isEqual:NSToolbarPrintItemIdentifier]) {

        // always enable print for this window
        enable = YES;
    }
    return enable;
}

您还可以使用actiontag确定要验证的工具栏项目.每当激活您的应用程序或调度事件时,项目都会被频繁验证,因此它们将始终处于有效状态.

You can also use action or tag to determine what toolbar item is being validated. Items are validated frequently, whenever your app is activated or events are dispatched, so they will always be in a valid state.

这篇关于如何启用/禁用NSToolbarItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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