如何使菜单栏项显示表中有多少行?

时间:2009-05-05 06:12:01

标签: cocoa core-data

我有一个核心数据表,并希望菜单栏项显示表中有多少行。我已经使用以下代码创建了菜单栏项:

 -(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 个答案:

答案 0 :(得分:4)

如果您不需要实时更新,可以尝试这种方法:

1)设置theMenu的代表:

[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。