Objective c 检查NSStatusItem当前是否显示在OS X NSStatusBar主菜单栏中
我的应用程序将NSStatusItem放入OS X菜单栏。在某个时候,我想从系统状态栏中删除菜单栏图标。(此时我仍希望保留NSStatusItem,并向其发送消息…只是不显示它。) 我正在使用此方法从状态栏中删除statusItem:Objective c 检查NSStatusItem当前是否显示在OS X NSStatusBar主菜单栏中,objective-c,macos,cocoa,menubar,nsstatusitem,Objective C,Macos,Cocoa,Menubar,Nsstatusitem,我的应用程序将NSStatusItem放入OS X菜单栏。在某个时候,我想从系统状态栏中删除菜单栏图标。(此时我仍希望保留NSStatusItem,并向其发送消息…只是不显示它。) 我正在使用此方法从状态栏中删除statusItem: [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; 稍后,我想检查statusItem当前是否显示在状态栏中。我不希望通过布尔或等来跟踪这一点 我想这张支票会有用的: if ([[NSStat
[[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的状态栏中没有明显的变化
似乎不包含任何适用的方法
有没有办法检查某个NSStatusItem是否在主系统栏中?我找到了私有属性
\u statusItems
这是我写的一个小类别,我不确定它是否有效,但你可以试试
状态栏类别
编辑
应考虑添加<代码> BOOL标志,而不是访问私有方法。
我的分类只是一个例子,如果你想将你的应用上传到MAS,你通常不应该使用私有方法。谢谢你,但我会避免使用私有方法,因为这个应用是针对Mac应用商店的。
BOOL
标志似乎是最好的可用方法。
@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