删除Cocoa应用程序中的"打开最近"菜单项

Ada*_*dam 4 macos cocoa objective-c nsmenu

我找到了一群人问这个(删除或禁用最近的项目子菜单),没有答案.

经过一番调查......问题是Apple已秘密硬编码该特定菜单总是出现 - 即使你删除它,NSWindowController也会默默地重新创建它.

Ada*_*dam 5

编辑:一些白痴感觉像重写我的答案.别.或者我会删除它.根据最初拒绝编辑的评论者:"此编辑太小;建议编辑应该是解决帖子中多个问题的实质性改进." 所以:不要.


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

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

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

@property( nonatomic, retain) IBOutlet NSMenu* fileMenu;
Run Code Online (Sandbox Code Playgroud)

...并确保你在MainWindow.xib中有你的AppDelegate类(例如使用蓝色立方体对象,并将类设置为你的AppDelegate所在的类)...所以你可以连接NIB内的文件菜单本身直接发给您的app-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];
}
Run Code Online (Sandbox Code Playgroud)