views:

58

answers:

2

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?

A: 

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.

Chris Becke
AppKit is part of Cocoa, and you should import system framework headers (at least the ones you use everywhere) in your prefix header, as the Xcode project templates do for you. Moreover, this doesn't answer the question, which was about Xcode not offering NSMenu instance method completions when using an `NSMenu *` variable.
Peter Hosey
Well I don't know about that. the point is, when I instantiate my menu, method completions seem working just fine. Perhaps an alternate syntax is all thats required to get XCode to parse the code properly.
Chris Becke
A: 

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.

Peter Hosey
The bottom line is that Xcode's autocomplete is a little flaky. I'll frequently use methods that never autocomplete, no matter how much of them I type out... but as soon as I hit that last character they turn blue. Xcode tries to play it off like it knew what you meant to type the whole time.
kubi