views:

141

answers:

1

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?

+3  A: 

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.

Markus Müller
I see, so where would I enter that code?
Joshua
Just to confirm, this will display the Number of rows in the Menu Bar At The Top Of The Screen not in the Drop Down Menu. Right?
Joshua
#1 Probably into the same controller that manages your status item.#2 It will display it in a menu item when you click on the status item. If you want to change the title of the status item it's probably best to register yourself as an observer of the array controller (I think arrangedObjects should work in this case) and set the title in the observeValueForKeyPath:ofObject:change:context: methode.
Markus Müller
Ok, Thanks, so what code would i use to make it change the title of the status item?
Joshua