在iOS开发中为了后期维护的便利多数人都建议纯代码开发,可以从iOS转战macOS的小伙伴们都建议直接用storyboard
或者XIB
开发,原因是macOS开发中很多逻辑代码的调用都需要手动去调用,不像UIKit里面很多初始化步骤系统内部都做好了,但是从学习的角度来说我个人建议纯代码开始入手,当然难度也不小,资料很少等,但是办法总比困难多,我们今天进军顶部菜单栏,这里只介绍搭建顶部菜单栏的搭建布局,内部系统方法的调用比较多,目前了解有限,请大家见谅
动工:直接上代码
在info.plist文件里设置如下:
<key>AppMenu</key>
<array>
<dict>
<key>items</key>
<array>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>About Application</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>Preference...</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>quite</string>
<key>selector</key>
<string>terminate:</string>
<key>target</key>
<string>NSApp</string>
</dict>
</array>
<key>name</key>
<string>Application</string>
<key>selector</key>
<string></string>
<key>target</key>
<string></string>
</dict>
<dict>
<key>items</key>
<array>
<dict>
<key>items</key>
<array>
<dict>
<key>items</key>
<array>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>MiniZoome</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
</array>
<key>name</key>
<string>MiniZoome</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
</array>
<key>name</key>
<string>MiniZoome</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>Zoom</string>
<key>selector</key>
<string>orderFrontStandardAboutPanel:</string>
<key>target</key>
<string>NSApp</string>
</dict>
<dict>
<key>items</key>
<array/>
<key>name</key>
<string>Help</string>
<key>selector</key>
<string>terminate:</string>
<key>target</key>
<string>NSApp</string>
</dict>
</array>
<key>name</key>
<string>Window</string>
<key>selector</key>
<string></string>
<key>target</key>
<string></string>
</dict>
</array>
代码如下
#pragma mark 根据info.plist AppMenu设置,创建菜单
-(void)defaultMenu{
NSMenu *mainMenu = [[NSMenu alloc] init];
for (NSDictionary *item in [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppMenu"]) {
NSMenuItem *item1 = [[NSMenuItem alloc] initWithTitle:[item objectForKey:@"name"] action:nil keyEquivalent:@""];
[mainMenu addItem:item1];
[mainMenu setSubmenu:[self createMenu:[item objectForKey:@"name"] withItem:[item objectForKey:@"items"]] forItem:item1];
}
NSApp.mainMenu = mainMenu;
return;
}
-(NSMenu *)createMenu:(NSString *)name withItem:(NSArray *)itemDict{
NSMenu *menu = [[NSMenu alloc] initWithTitle:name];
for (NSDictionary *dict in itemDict) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[dict objectForKey:@"name"] action:nil keyEquivalent:@""];
item.target = NSClassFromString([dict objectForKey:@"target"]);
item.action = NSSelectorFromString([dict objectForKey:@"selector"]);
[menu addItem:item];
//分割线
[menu addItem:[NSMenuItem separatorItem]];
if([[dict objectForKey:@"items"] count] >0){
[menu setSubmenu:[self createMenu:[dict objectForKey:@"name"] withItem:[dict objectForKey:@"items"]] forItem:item];
}
}
return menu;
}
完结