Mac开发 - 使用CCMenu和CCMenuItem添加菜单、右击菜单、Dock菜单 1、添加一个Dock右击菜单 NSMenu *appDockMenu = [[NSMenu alloc] initWithTitle:@"DockMenu"]; [appDockMenu setAutoenablesItems:NO]; NSMenuItem* newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"About" action:@selector(aboutDockAction:) keyEquivalent:@""]; [newItem setTarget: self]; [appDockMenu addItem:newItem]; [newItem release]; - (NSMenu *)applicationDockMenu:(NSApplication *)sender { return appDockMenu; } 2、给一个NSView添加右击菜单 NSMenu* newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@"Copy"]; NSMenuItem* newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Copy" action:@selector(copyAction:) keyEquivalent:@""]; [newItem setEnabled:YES]; [newItem setTarget:self]; [newMenu addItem:newItem]; [newItem release]; [myview setMenu:newMenu]; [newMenu release]; 3、给顶部菜单栏,添加菜单 NSMenu* newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@"About"]; NSMenuItem* newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"About"action:@selector(copyAction:) keyEquivalent:@""]; [newItem setEnabled:YES]; [newItem setTarget:self]; [newMenu addItem:newItem]; [newItem release]; [[NSApp mainMenu] insertItem:newItem atIndex:3]; [newMenu release];
NSMenu *subMenu = [[NSMenu alloc]init]; for (NSDictionary *dic in folderArray) { for (NSString *folderKey in [dic allKeys]) { NSDictionary *folderInfo = [dic objectForKey:folderKey]; NSString *appName = [folderInfo objectForKey:@"APP_NAME"]; IBDMenuItem* newItem = [[IBDMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle: appName action:@selector(moveToAction:) keyEquivalent:@""]; //绑定选择移动到item newItem.tag = folderKey.intValue; //绑定要移动的Item 考虑传值问题 newItem.selfIndex = index.integerValue; [newItem setTarget: self]; [subMenu insertItem:newItem atIndex:0]; }