我有一个核心数据表,希望菜单栏项显示表中有多少行。我已经使用以下代码创建了菜单栏项:
-(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) 设置菜单的委托(delegate):
[theMenu setDelegate:self];
2)并实现委托(delegate)方法:
- (void)menuWillOpen:(NSMenu *)menu {
NSUInteger count = [self.tableView numberOfRows];
[[menu itemAtIndex:0] setTitle: [NSString stringWithFormat:@"%d rows", count]];
}
每次用户打开菜单时,此代码都会刷新菜单项。如果你想在每次表中的内容发生变化时刷新它,你将需要使用 KVO 来观察数组 Controller 。如果您想在 StatusItem 的标题中显示计数,您还需要使用 KVO。
关于cocoa - 如何使菜单栏项显示表格中有多少行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/823570/