1

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

RuLoViC
  • 825
  • 7
  • 23
  • Is there more to this application, such as creating an NSApplication instance (which controls the main event loop)? – red_menace Nov 10 '20 at 21:52

1 Answers1

3

-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;
}
apodidae
  • 1,988
  • 2
  • 5
  • 9