11

我有一个项目需要禁用/启用某些NSToolbarItems 取决于不同的选项。我检查并发现没有参数。

有没有办法启用/禁用给定的NSToolbarItem

4

4 回答 4

10

在您的窗口、视图或文档控制器中实现 NSToolbarItemValidation 协议。该文档提供了以下示例代码:

-(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来确定正在验证的工具栏项。每当您的应用程序被激活或事件被调度时,项目都会被频繁验证,因此它们将始终处于有效状态。

于 2011-11-05T03:49:19.250 回答
8

有一个更简单的解决方案:

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

    return [toolbarItem isEnabled] ;
}

这样你就可以使用 [yourToolBarItem setEnabled:YES/NO] ;在你的代码中。

于 2013-11-22T14:17:39.413 回答
2

快速执行此操作的一种简单方法,或者您可以将其移植到目标 c 只是设置操作

这会禁用该项目

Mytoolbar.action = nil

这重新启用它

Mytoolbar.action = "Name of a function"

在这样做时,你会想用函数替换你的 IBAction

@IBAction func blehbleh(sender: AnyObject){ Stuff }

将更改为

func blehbleh(){ Stuff }
于 2014-12-06T10:10:50.957 回答
0

正如 nsij22 所说,您需要设置操作。

在 Storyboard 中,只需 ctrl+从工具栏项拖动到您的代码操作。

于 2015-07-17T17:11:00.400 回答