Xcode completion doesn't recognize the type of my variable
I'm creating an object with:
NSMenu *appMainMenu = [[NSMenu alloc] initWithTitle:@"MyApp"];
(Ignore the fact I'm creating this menu programmatically and not using a Nib File. I understand the disadvantages of doing so)
The menu appears correctly in the menubar.
However, when I try to call any instance method such as:
[appMainMenu addItemWithTitle:@"MyTitle开发者_运维技巧" action:@selector(myaction:) keyEquivalent:@"t"];
XCode offer some completions, but none appear to come from NSMenu.
I've tried both#import <AppKit/AppKit.h> and #import <AppKit/NSMenu.h>
The instance methods are certainly there in NSMenu.h, and as I said, it installs my menu. It just doesn't install the menu item. That plus the lack of completions makes me think that my appMainMenu is not being recognized as a NSMenu object, even though it's obviously valid.
What obvious thing am I missing?
I'm creating an object with:
NSMenu *appMainMenu = [[NSMenu alloc] initWithTitle:@"MyApp"];
You should allocate it from [NSMenu menuZone]
.
(It's the same zone as the default as of 10.6.1, but as long as the documentation says you should use [NSMenu menuZone]
, you probably should use [NSMenu menuZone]
.)
However, when I try to call any instance method such as:
[appMainMenu addItemWithTitle:@"MyTitle" action:@selector(myaction:) keyEquivalent:@"t"];
XCode offer some completions, but none appear to come from NSMenu.
First, it's Xcode, with a lowercase c.
Try saving. Sometimes Xcode doesn't realize I've created a variable until I save the file, thereby provoking it to rebuild whatever the completions are coming from.
I start by creating an empty main menu, then attaching the menu items to it :-
// I am also entirely unsure about the difference between
// using AppKit directly vs the Cocoa framework
#import <cocoa/cocoa.h>
// create an empty main menu and set it as the apps main menu
[NSApp setMainMenu:[[NSMenu alloc] init]];
// The first (sub)menu of the app menu is always the app menu and is named automatically
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:@""];
// Now, add an about entry
[appMenu addItemWithTitle:@"About MyApp" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
This works for me.
精彩评论