Re: Creating a Cocoa Window in code
Re: Creating a Cocoa Window in code
- Subject: Re: Creating a Cocoa Window in code
- From: "Felipe Monteiro de Carvalho" <email@hidden>
- Date: Fri, 18 Jan 2008 13:48:53 +0100
Hi, thanks, I think I have it almost working! At least for a initial
hello world. I managed to get to the message loop by calling NSApp
run, but then there is no way to quit the application, so I added a
simple menu too. I can't find anything wrong with my menu
implementation, but when I select the Quit menu I get an error on the
console:
2008-01-18 13:45:06.743 cocoawindow[797] could not find associated
NSMenu for -20213 (item:9)
Any ideas? Here is my code, thanks:
#import <Cocoa/Cocoa.h>
void CreateApplicationMenu(NSMenu* menu)
{
NSString * applicationName = @"cocoawindow";
NSMenuItem * item;
item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
NSLocalizedString(@"Quit", nil), applicationName]
action:@selector(terminate:)
keyEquivalent:@"q"];
[item setTarget:NSApp];
}
void CreateMenu(void)
{
NSMenu * mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
NSMenuItem * item;
NSMenu * submenu;
// The titles of the menu items are for identification purposes only
and shouldn't be localized.
// The strings in the menu bar come from the submenu titles,
// except for the application menu, whose title is ignored at runtime.
item = [mainMenu addItemWithTitle:@"Apple" action:NULL keyEquivalent:@""];
submenu = [[[NSMenu alloc] initWithTitle:@"Apple"] autorelease];
[NSApp performSelector:@selector(setAppleMenu:) withObject:submenu];
CreateApplicationMenu(submenu);
[mainMenu setSubmenu:submenu forItem:item];
}
int main(int argc, char *argv[])
{
// Creates an autorelease pool
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Loads the application
NSApplicationLoad();
// Creates a simple window
NSRect MainWindowRect = NSMakeRect(300, 300, 300, 500);
NSWindow* MainWindow = [[NSWindow alloc] initWithContentRect: MainWindowRect
styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[MainWindow orderFrontRegardless];
// Creates the application object
[NSApplication sharedApplication];
CreateMenu();
// Enters main message loop
[NSApp run];
// Call release method from object pool
[pool release];
return 0;
}
--
Felipe Monteiro de Carvalho
_______________________________________________
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