objective-c - 删除 Cocoa 应用程序中的 "Open Recent"菜单项

标签 objective-c cocoa macos nsmenu

我发现很多人都在问这个问题(删除或禁用最近的项目子菜单)但没有人回答。

经过一些调查……问题是 Apple 已经 secret 地硬编码了那个特定的菜单以总是出现——即使你删除它,NSWindowController 也会默默地重新创建它。

最佳答案

编辑:有些白痴想要改写我的回答。不。或者我会删除它。根据最初拒绝编辑的审稿人的说法:“这个编辑太小了;建议的编辑应该是实质性的改进,以解决帖子中的多个问题。”所以:不要。


Apple 有一个官方的解决方法(他们勉强接受他们在硬编码菜单时的错误):

http://developer.apple.com/library/mac/#qa/qa2001/qa1289.html

似乎工作正常,一旦你设置了一个 IBOutlet:

@property( nonatomic, retain) IBOutlet NSMenu* fileMenu;

...并确保您的 AppDelegate 类在 MainWindow.xib 中表示(例如,使用蓝色立方体对象,并将该类设置为您的 AppDelegate 所在的任何类)...这样您就可以连接文件菜单本身在 NIB 内直接连接到您的应用程序委托(delegate)。

编辑:实际上,修改 - Apple 的源代码无法使用 Xcode4 正确编译,生成编译器警告。你想要这个:

NSInteger openDocumentMenuItemIndex = [self.fileMenu indexOfItemWithTarget:nil andAction:@selector(openDocument:)];

if (openDocumentMenuItemIndex>=0 &&
    [[self.fileMenu itemAtIndex:openDocumentMenuItemIndex+1] hasSubmenu])
{
    // APPLE'S COMMENT: We'll presume it's the Open Recent menu item, because this is
    // APPLE'S COMMENT: the heuristic that NSDocumentController uses to add it to the
    // APPLE'S COMMENT: File menu
    [self.fileMenu removeItemAtIndex:openDocumentMenuItemIndex+1];
}

关于objective-c - 删除 Cocoa 应用程序中的 "Open Recent"菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6307521/

相关文章:

swift - Xcode 8.1 的新自动布局错误

objective-c - 在 cocoa 窗口 Controller 中获取鼠标事件

macos - “导出 : command not found

macos - 如何打包 Mac OS 应用程序进行安装?

c - 如何不忽略 dyld 的最大版本

iphone - 外观 API - UIBarButtonItem - MPMoviePlayerViewController 和 Youtube Webview

ios - Xcode 9 : infinite calculation of auto-layout constraints leads to OOM 中的 UINavigationController 方法 setToolbarHidden 错误

iphone - 在 Objective-C 中处理数组的更好方法?

objective-c - Xcode 在断点处停止时不显示代码

objective-c - 关闭 NSWindow 并释放内存,但在单击按钮时重新打开它