【问题标题】:Check if an NSStatusItem is currently shown in the OS X NSStatusBar main menubar检查 NSStatusItem 当前是否显示在 OS X NSStatusBar 主菜单栏中
【发布时间】:2012-12-31 18:31:42
【问题描述】:

我的应用在 OS X 菜单栏中放置了一个 NSStatusItem。在某些时候,我想从系统 NSStatusBar 中删除菜单栏图标。 (此时我仍想保留 NSStatusItem,并向其发送消息……只是没有显示。)

我正在使用这种方法从状态栏中删除状态项:

[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];

我想稍后检查 statusItem 当前是否显示在状态栏中。我不希望通过布尔值等来跟踪这一点。

我认为这个检查会起作用:

if ([[NSStatusBar systemStatusBar] isEqualTo:[statusItem statusBar]])
{
    NSLog(@"statusItem's bar == system bar, before");
}

NSLog(@"removing from systemStatusBar");
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];

if ([[NSStatusBar systemStatusBar] isEqualTo:[statusItem statusBar]])
{
    NSLog(@"statusItem's bar == system bar, after removal");
}

这个输出:

statusItem's bar == system bar, before removal

removing from systemStatusBar

statusItem's bar == system bar, after removal

所以statusItem的状态栏没有明显变化。

NSStatusBar class reference 似乎不包含任何适用的方法。

有没有办法检查某个 NSStatusItem 是否在主系统栏中?

【问题讨论】:

    标签: objective-c macos cocoa menubar nsstatusitem


    【解决方案1】:

    我找到了私有财产_statusItems
    这是我写的一个小分类,不知道好不好用,大家可以试试。


    状态栏类别

    @implementation NSStatusBar (statusItemCheck)
    - (NSArray *)items {
        return [self valueForKey:@"_statusItems"];
    }
    - (BOOL)statusItemIsShown:(NSStatusItem *)statusItem {
        if ([self items]) {
            NSInteger index = [[self items] indexOfObject:statusItem];
            if (index != -1) return YES;
        }
    
        return NO;
    }
    @end
    

    编辑

    您应该考虑添加BOOL 标志,而不是访问私有方法。
    我的类别只是一个例子,如果你想将你的应用上传到 MAS,你一般不应该使用私有方法。

    【讨论】:

    • 谢谢你,但我会避免使用私有方法,因为这个应用程序是针对 Mac App Store 的。 BOOL 标志似乎是最好的可用方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多