我有一个项目,需要禁用/启用一些NSToolbarItem取决于不同的选项。我检查了一下,没有找到这个的参数。
有没有办法启用/禁用给定的NSToolbarItem
发布于 2011-11-05 11:49:19
在您的窗口、视图或文档控制器中实现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;
}您还可以使用action或tag来确定正在验证的工具栏项。每当您的应用程序被激活或事件被调度时,项都会频繁地进行验证,因此它们始终处于有效状态。
发布于 2013-11-22 22:17:39
有一个更简单的解决方案:
-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem
{
return [toolbarItem isEnabled] ;
}这样,您就可以在代码中使用yourToolBarItem setEnabled:YES/NO;。
发布于 2014-12-06 18:10:51
在swift中实现这一点的一种简单方法,或者您可以将其移植到目标c中,就是设置操作。
这将禁用该项目
Mytoolbar.action = nilThis reEnables it
Mytoolbar.action = "Name of a function"在执行此操作时,您可能希望使用以下函数替换您的IBAction
@IBAction func blehbleh(sender: AnyObject){ Stuff }将更改为
func blehbleh(){ Stuff }https://stackoverflow.com/questions/8017822
复制相似问题