Remove the "Open Recent" menu item in Cocoa apps
I found a bunch of people asking this (remove or disable the recent-items s开发者_运维技巧ubmenu) and no answers.
After a bit of investigation ... the problem is that Apple has secretly hard-coded that particular menu to always appear - even if you delete it, NSWindowController will silently re-create it.
EDIT: some idiot felt like rewording my answer. Don't. Or I'll delete it. As per the reviewer who rejected the edit originally: "This edit is too minor; suggested edits should be substantive improvements addressing multiple issues in the post." So: don't.
Apple has an official workaround (where they grudgingly accept their mistake in hardcoding the menu):
http://developer.apple.com/library/mac/#qa/qa2001/qa1289.html
Seems to work OK, once you setup an IBOutlet:
@property( nonatomic, retain) IBOutlet NSMenu* fileMenu;
...and make sure you have your AppDelegate class represented inside MainWindow.xib (e.g. using a blue-cube Object, and set the class to whatever class your AppDelegate is) ... so you can connect the File Menu itself inside the NIB direct to your app-delegate.
EDIT: actually, modification - Apple's source doesn't compile correctly with Xcode4, generating a compiler warning. You want this instead:
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];
}
精彩评论