Re: Creating the app menu from scratch
Re: Creating the app menu from scratch
- Subject: Re: Creating the app menu from scratch
- From: Jeff Johnson <email@hidden>
- Date: Sun, 13 Jul 2008 17:19:57 -0500
Jim,
I wrote a series of 6 posts on my blog exploring this subject.
http://lapcatsoftware.com/blog/?s=working+without+a+nib
There are code samples for both Tiger and Leopard.
-Jeff
On Jul 13, 2008, at 4:01 PM, Jim Crafton wrote:
I'd like to be able to create an application's menu from scratch,
without a nib file. I'm having problems doing so, I've looked around
and seen some different code samples but nothing seems to work
correctly.
The simplest case:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSApplication* app = [NSApplication sharedApplication];
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:@"My App"];
[appMenu addItemWithTitle:@"Item 1" action:nil keyEquivalent:@""];
NSMenu* menu = [[NSMenu alloc] initWithTitle: @""];
NSMenuItem* mi = [menu addItemWithTitle:@"" action:nil
keyEquivalent:@""];
[mi setSubmenu:appMenu];
//[NSApp performSelector:NSSelectorFromString(@"setAppleMenu:")
withObject:menu];
//[app setAppleMenu:menu];
[app setMainMenu:menu];
[[NSApplication sharedApplication] run];
[pool release];
return 0;
}
Assuming my project is called "test7", this sort of works. It ends up
creating a menu item and a sub menu, but there's *still* a "test7"
menu item, just before the Apple menu item on the menu bar. Is there
something I'm missing? If I try and call setAppleMenu, that does
nothing. Is this even possible? Some of the code sample's I saw (from
several years ago) made references to functions in the NSMenu class
that don't seem to exist anymore, i.e. :
[self setMainMenu: [NSMenu menuWithTitle: @"" submenus: [NSArray
arrayWithObjects:
[self applicationMenu],
[self editMenu],
[self windowMenu],
[self helpMenu],
nil]]];
There doesn't seem to be a menuWithTitle static class function
anymore, or at least it's not documented.
I tried searching the mailing list but the searches kept failing, so
I'm emailing the list directly.
Thanks
Jim
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden