概要
本文共分為個階段,每個階段完成后都可以看到階段性成果痢士。
階段一彪薛、顯示Menu Bar小圖標。
階段二、隱藏程序窗口和啟動欄圖標善延。(無圖)
階段三少态、點擊MenuBar小圖標,出現(xiàn)菜單易遣。
階段四彼妻、點擊MenuBar小圖標,出現(xiàn)一個小窗口豆茫。
階段五侨歉、通過事件監(jiān)聽鼠標點擊,“智能”關(guān)閉小窗口揩魂。
階段一
1.創(chuàng)建工程幽邓,向工程中加入圖標,對圖標資源進行設(shè)置肤京。
2.在AppDelegate.h中添加NSStatusItem類型的指針變量颊艳,對其進行初始化操作,定義一個getDiamond函數(shù)忘分。
//AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@property NSStatusItem *statusItem;
- (void) getDiamond:(NSStatusBarButton *)sender;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//初始化_statusItem
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
//設(shè)置_statusItem按鈕相關(guān)信息
NSStatusBarButton *button = _statusItem.button;
if(button){
button.image = [NSImage imageNamed:@"zuan"];
button.action = @selector(getDiamond:);
}
}
- (void) getDiamond:(NSStatusBarButton *)sender {
NSLog(@"You get a lot of diamonds!");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
3.Command+R運行棋枕,效果如下。
階段二
1.打開storyboard妒峦,選中窗口重斑,取消勾選【Is initial Controller】便可去掉窗口。
2.打開工程設(shè)置肯骇,點擊【Info】窥浪,新增“Application is agent (UIElement)”選項,其值為YES笛丙。
3.階段二完成漾脂!
階段三
1.注釋掉AppDelegate.m中applicationDidFinishLaunching函數(shù)內(nèi)部的如下語句
//button.action = @selector(getDiamond:);
2.在applicationDidFinishLaunching函數(shù)中添加如下語句。
NSMenu *menu;
menu = [[NSMenu alloc] init];
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"Get Diamond" action:@selector(getDiamond:) keyEquivalent:@"g"] ];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"] ];
_statusItem.menu = menu;
3.AppDelegate.m修改后的完整版代碼如下胚鸯。
#import "AppDelegate.h"
@interface AppDelegate ()
@property NSStatusItem *statusItem;
- (void) getDiamond:(NSStatusBarButton *)sender;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//初始化_statusItem
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
//設(shè)置_statusItem按鈕相關(guān)信息
NSStatusBarButton *button = _statusItem.button;
if(button){
button.image = [NSImage imageNamed:@"zuan"];
//button.action = @selector(getDiamond:);
}
NSMenu *menu;
menu = [[NSMenu alloc] init];
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"Get Diamond" action:@selector(getDiamond:) keyEquivalent:@"g"] ];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"] ];
_statusItem.menu = menu;
}
- (void) getDiamond:(NSStatusBarButton *)sender {
NSLog(@"You get a lot of diamonds!");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
4.階段三完成骨稿,效果如下。
階段四
1.添加一個ViewController的子類GetDiamonds姜钳,創(chuàng)建xib文件坦冠,聲明并完成其初始化方法。
//DiamondViewController.h
#import <Cocoa/Cocoa.h>
@interface DiamondViewController : NSViewController
- (id) initWithCustom;
@end
//DiamondViewController.m
- (id) initWithCustom{
self = [super initWithNibName:@"DiamondViewController" bundle:nil];
return self;
}
2.點擊DiamondViewController.xib文件哥桥,選擇label拖拽直View中央辙浑。
3.打開AppDelegate.m,新增一個NSPopver的指針變量拟糕,新增部分代碼判呕,完成后代碼如下倦踢。
#import "AppDelegate.h"
#import "DiamondViewController.h"
@interface AppDelegate ()
@property NSStatusItem *statusItem;
@property NSPopover *popover;
- (void) getDiamond:(NSStatusBarButton *)sender;
- (void)toggleWeather:(NSStatusBarButton *)sender
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//初始化_statusItem
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
//設(shè)置_statusItem按鈕相關(guān)信息
NSStatusBarButton *button = _statusItem.button;
if(button){
button.image = [NSImage imageNamed:@"zuan"];
//修改
button.action = @selector(toggleDiamond:);
}
// 將menu注釋掉。
// NSMenu *menu;
// menu = [[NSMenu alloc] init];
// [menu addItem:[[NSMenuItem alloc] initWithTitle:@"Get Diamond" action:@selector(getDiamond:) keyEquivalent:@"g"] ];
// [menu addItem:[NSMenuItem separatorItem]];
// [menu addItem:[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"] ];
// _statusItem.menu = menu;
//新增
_popover = [[NSPopover alloc] init];
_popover.contentViewController = [[DiamondViewController alloc] initWithCustom];
}
- (void) getDiamond:(NSStatusBarButton *)sender {
NSLog(@"You get a lot of diamonds!");
}
//新增
- (void)toggleDiamond:(NSStatusBarButton *)sender{
if([_popover isShown]){
[_popover performClose:sender];
}else{
NSStatusBarButton *button = _statusItem.button;
if(button){
[_popover showRelativeToRect:button.bounds ofView:button preferredEdge:NSRectEdgeMinY];
}
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
4.階段四完成佛玄,效果如下硼一。
階段五
1.新建NSObject的子類EventMonitor,代碼如下梦抢。
//EventMonitor.h
import <Cocoa/Cocoa.h>
@interface EventMonitor : NSObject
@property id monitor;
@property NSEventMask mask;
@property void (^handler)(NSEvent *);
- (id) initWithMaskAndHandler:(NSEventMask)mask handler:(void (^)(NSEvent *))handler;
- (void) start;
- (void) stop;
@end
//EventMonitor.m
#import "EventMonitor.h"
@implementation EventMonitor
- (id) initWithMaskAndHandler:(NSEventMask)mask handler:(void (^)(NSEvent *)) handler {
self = [super init];
if(self){
_mask = mask;
_handler = handler;
}
return self;
}
- (void) start{
_monitor = [NSEvent addGlobalMonitorForEventsMatchingMask:_mask handler:_handler];
}
- (void) stop{
if(_monitor != nil ){
[NSEvent removeMonitor:_monitor];
_monitor = nil;
}
}
@end
3.打開AppDelegate.m般贼,添加EventMonitor的成員變量,并對其進行初始化奥吩。
@property EventMonitor* eventMonitor;
_eventMonitor = [[EventMonitor alloc] initWithMaskAndHandler:NSEventMaskLeftMouseUp|NSEventMaskRightMouseUp handler:^(NSEvent *h) {
if([_popover isShown]){
[self closePopover:h];
}
}];
4.對代碼進行修改哼蛆,新增closePopover和showPopover函數(shù),修改toggleDiamond函數(shù)部分代碼霞赫。
//新增
-(void) closePopover:(id)sender{
[_popover performClose:sender];
[_eventMonitor stop];
}
//新增
-(void) showPopover:(id)sender{
NSStatusBarButton *button = [_statusItem button];
if(button){
[_popover showRelativeToRect:button.bounds ofView:button preferredEdge:NSRectEdgeMinY];
}
[_eventMonitor start];
}
-(void) toggleDiamond:(NSStatusBarButton *)sender{
if([_popover isShown]){
//修改
[self closePopover:sender];
}else{
//修改
[self showPopover:sender];
}
}
5.階段五完成腮介,效果如下。
結(jié)語
想了解更多關(guān)于Popover的用法端衰,詳見:一步一步熟悉Mac app開發(fā)(九)之NSPopover