Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation StatusItemController
- @synthesize theMenu;
- @synthesize statusItem;
- - (id)init {
- if ((self = [super init])) {
- secondsPassed = 0;
- NSStatusBar *bar = [NSStatusBar systemStatusBar];
- statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
- // Not sure if this retain is necessary...hmmm
- [statusItem retain];
- [statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
- [statusItem setHighlightMode:YES];
- // Create your menu and add buttons/dividers to it. Adding submenus also works.
- theMenu = [[NSMenu alloc] init];
- // Had to disable this to get it to work properly;
- // there's probably a good reason why I couldn't get it working with it enabled.
- [theMenu setAutoenablesItems:NO];
- // It's always good form to include a "Quit" button
- NSMenuItem *quitItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(quit:) keyEquivalent:@""];
- [quitItem setTarget:self];
- [quitItem setEnabled:YES];
- [theMenu addItem:quitItem];
- [quitItem release];
- [statusItem setMenu:theMenu];
- // And here is where you would start your background process...
- // EXAMPLE:
- NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
- target: self
- selector: @selector(updateLabel:)
- userInfo: nil
- repeats: YES];
- [timer fire];
- }
- return self;
- }
- - (void)updateLabel:(id)sender {
- // Change the status bar thing.
- secondsPassed += 1;
- [statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
- }
- - (void)dealloc {
- [theMenu release];
- [statusItem release];
- [super dealloc];
- }
- - (void)quit:(id)sender {
- [[NSApplication sharedApplication] terminate:self];
- }
Add Comment
Please, Sign In to add comment