2014-04-14 84 views
0

我的應用程序有2個狀態欄圖標。 (StatusItem)如何從代碼中刪除statusbaritem?

第一個是永遠在線。 第二個只顯示一個功能。

我的問題與此代碼

-(void)deleteStatusBarItem 
{ 
    NSStatusBar *bar = [NSStatusBar systemStatusBar]; 
    [bar removeStatusItem:statusBarItem]; 
} 

如果我不上我的自定義狀態項視圖中單擊它不工作開始。

這裏代碼我以通常的方式觸發我的菜單。

-(void)mouseDown:(NSEvent *)event { 
    _isPressed = YES; 
    [self setNeedsDisplay:YES]; 
    [self performSelector:@selector(afterDelayCall) withObject:nil afterDelay:0]; 
} 
-(void)afterDelayCall 
{ 
    [[_statusItem menu] setDelegate:(id)self]; 
    [_statusItem popUpStatusItemMenu:[_statusItem menu]]; 
} 

這裏是init的代碼。以防萬一。

-(void)addStatusBarItem 
{ 
    if (!statusBarItem) 
    { 
     NSStatusBar* bar = [NSStatusBar systemStatusBar]; 
     statusBarItem = [bar statusItemWithLength:60]; 

     trayMenu = [NSMenu new]; 

     NSMenuItem* startRecordingItem = [[NSMenuItem alloc] initWithTitle:@"Start recording" action:@selector(testfunc) keyEquivalent:@""]; 
     [startRecordingItem setTarget:self]; 
     [trayMenu addItem:startRecordingItem]; 

     NSMenuItem* closeRecordingItem = [[NSMenuItem alloc] initWithTitle:@"Close recorder" action:@selector(hideRecordWindow) keyEquivalent:@""]; 
     [closeRecordingItem setTarget:self]; 
     [trayMenu addItem:closeRecordingItem]; 

     goToFullScreenItem = [[NSMenuItem alloc] initWithTitle:@"Fullscreen mode" action:@selector(fullScreenAction) keyEquivalent:@""]; 
     [goToFullScreenItem setTarget:self]; 
     [trayMenu addItem:goToFullScreenItem]; 

     [statusBarItem setMenu:trayMenu]; 

     VRICustomStatusBarView* contentView = [VRICustomStatusBarView new]; 
     contentView.seconds = 0; 
     contentView.timeStringToDraw = @"00:00"; 
     contentView.statusItem = statusBarItem; 
     [statusBarItem setView:contentView]; 
     [contentView performSelectorOnMainThread:@selector(firstInitHack) withObject:nil waitUntilDone:1]; 
    } 
} 

我試圖模擬鼠標點擊或彈出菜單,但它沒有幫助。

-(void)firstInitHack 
{ 
    CGKeyCode key_code = 125; // kVK_DownArrow = 125 
    CGEventRef event1, event2; 
    event1 = CGEventCreateKeyboardEvent(NULL, key_code, YES); 
    event2 = CGEventCreateKeyboardEvent(NULL, key_code, NO); 
    CGEventPost(kCGSessionEventTap, event1); 
    CGEventPost(kCGSessionEventTap, event2); 
    CFRelease(event1); 
    CFRelease(event2); 

    NSEvent* ev = [NSEvent eventWithCGEvent:event1]; 
    [NSMenu popUpContextMenu:[_statusItem menu] withEvent:ev forView:self]; 
    [NSTimer scheduledAnimationTimerWithTimeInterval:0.2f target:self selector:@selector(removeStatusItemAfterDelay) userInfo:nil repeats:NO]; 
} 

-(void)removeStatusItemAfterDelay 
{ 
    [[_statusItem menu] cancelTracking]; 
} 

任何建議,這是怎麼回事)

UPDATE:

也許這個示例項目可以幫助? http://monosnap.com/file/RFhixVcSuKaUQQtxN7R0U0k6ij81ez

回答