Question

I have a core data table and would like the Menu Bar item to display how many rows there are in the table. I have already created the menu bar item using this code:

 -(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.
}

What do I need to add to make the Menu Bar item display how many rows there are in the table?

Was it helpful?

Solution

If you don't require live updating you can try this approach:

1) set the delegate of theMenu:

[theMenu setDelegate:self];

2) and implement the delegate methode:

- (void)menuWillOpen:(NSMenu *)menu {
    NSUInteger count = [self.tableView numberOfRows];
    [[menu itemAtIndex:0] setTitle: [NSString stringWithFormat:@"%d rows", count]];
}

This code will refresh the menu item every time the user opens the menu. If you want to refresh it every time something in the table changes, you will need to use KVO to observe the array controller. You will also need to use KVO if you want to display the count in the title of the StatusItem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top