
NSApplicationDelegate
实现
- (NSMenu *)applicationDockMenu:(NSApplication *)sender
{
return _menu;
}
2 状态条菜单
需要NSMenuDelegate protocol
@protocol NSMenuDelegate <NSObject>
@optional
- (void)menuNeedsUpdate:(NSMenu*)menu;
- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu;
- (BOOL)menu:(NSMenu*)menu updateItem:(NSMenuItem*)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel;
- (void)menuWillOpen:(NSMenu *)menu;
- (void)menuDidClose:(NSMenu *)menu;
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
@end

给StatusBar加Icon
- (void)activateStatusMenu:(int)status
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
icon = [bar statusItemWithLength:NSVariableStatusItemLength];
[icon retain];
NSString* filePath;
switch (status) {
case 0:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_complete" ofType:@"png"];
break;
case 1:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_processing" ofType:@"png"];
break;
case 2:
filePath = [[NSBundle mainBundle] pathForResource:@"sync_error" ofType:@"png"];
break;
default:
break;
}
NSImage* image = [[NSImage alloc] initWithContentsOfFile:filePath];
[icon setImage:image];
[icon setHighlightMode:YES];
[icon setMenu:mainMenu];
}
关于菜单就讲这么多

Leave a comment