I am creating simple Cocoa application from scratch, just to learn the process.
However I am not able to create Application Menu. I have this code:
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
[self createMenu];
}
-(void)createMenu
{
NSMenu* myMenu = [[NSMenu alloc] init];
NSString* quitTitle = @"Quit";
NSMenuItem* quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:)
keyEquivalent:@"q"];
[myMenu addItem:quitMenuItem];
[NSApp setMainMenu:myMenu];
}
However after launching the app when I click on the App name in the menu bar nothing happens
What am I missing please?
Thanks in advance
-createMenu() needs to be called from ApplicationWillFinishLaunching: and the menu itself has a few errors.
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
- (void)createMenu;
@end
@implementation AppDelegate
-(void)createMenu {
NSMenu *menubar = [[NSMenu alloc]init];
NSMenuItem *menuBarItem = [[NSMenuItem alloc] init];
[menubar addItem:menuBarItem];
[NSApp setMainMenu:menubar];
NSMenu *myMenu = [[NSMenu alloc]init];
NSString* quitTitle = @"Quit";
NSMenuItem* quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"];
[myMenu addItem:quitMenuItem];
[menuBarItem setSubmenu:myMenu];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self createMenu];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
}
@end
int main (){
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}