我有一个核心数据表,希望菜单栏项显示表中有多少行。我已经使用以下代码创建了菜单栏项:
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSStatusItem *statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; //Create new status item instance
[statusItem setHighlightMode:YES]; //This does something, I'm sure of it.
[statusItem setTitle:[NSString stringWithFormat:@"%C",0xff50]]; //This labels it. You can also use setImage instead to use an icon. That current code will result in a item labeled "p"
[statusItem setEnabled:YES]; //Self explanatory
[statusItem setMenu:theMenu];
[statusItem setToolTip:@"TOOLTIP HA AWESOME AMIRITE?"]; //Optional, just for kicks.
}
我需要添加什么以使菜单栏项显示表中有多少行?
最佳答案
如果您不需要实时更新,可以尝试以下方法:
1)设置Menu的委托:
[theMenu setDelegate:self];
2)并实现委托方法:
- (void)menuWillOpen:(NSMenu *)menu {
NSUInteger count = [self.tableView numberOfRows];
[[menu itemAtIndex:0] setTitle: [NSString stringWithFormat:@"%d rows", count]];
}
每次用户打开菜单时,此代码都会刷新菜单项。如果要在表中的某些内容每次更改时刷新它,则需要使用KVO观察阵列控制器。如果要在StatusItem的标题中显示计数,则还需要使用KVO。