如何从代码中删除statusbaritem?

时间:2014-04-14 17:50:34

标签: macos cocoa statusbar

我的应用有2个状态栏图标。 (StatusItem)

第一个始终开启。 第二个只显示一个功能。

我的问题始于此代码

-(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];
}

任何建议,发生了什么?)

更新:

也许这个示例项目可以提供帮助吗? http://monosnap.com/file/RFhixVcSuKaUQQtxN7R0U0k6ij81ez

1 个答案:

答案 0 :(得分:0)

这是一个小牛队的错误。 Apple支持给我解决方法。

- (IBAction)removeStatusItem:(id)sender {

    [statusBarItem setView:nil];
    NSStatusBar *bar = [NSStatusBar systemStatusBar];
    [bar removeStatusItem:statusBarItem];
    statusBarItem = nil;
}

这里有一些关于它的讨论:https://devforums.apple.com/message/959387#959387 Apple支持部门的回答是:http://monosnap.com/image/tjNKlyF8bF1Kdl0sfpVkZErdZUgURV