如何以编程方式创建 Cocoa app 主菜单应用程序
How to create Cocoa app main menu application programatically
我正在从头开始创建简单的 Cocoa 应用程序,只是为了学习这个过程。
但是我无法创建应用程序菜单。我有这个代码:
- (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];
}
但是当我点击菜单栏中的应用程序名称启动应用程序后没有任何反应
请问我错过了什么?
提前致谢
-createMenu() 需要从 ApplicationWillFinishLaunching: 调用,菜单本身有一些错误。
#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;
}
我正在从头开始创建简单的 Cocoa 应用程序,只是为了学习这个过程。
但是我无法创建应用程序菜单。我有这个代码:
- (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];
}
但是当我点击菜单栏中的应用程序名称启动应用程序后没有任何反应
请问我错过了什么?
提前致谢
-createMenu() 需要从 ApplicationWillFinishLaunching: 调用,菜单本身有一些错误。
#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;
}