Created
January 8, 2013 00:37
-
-
Save othercat/4479978 to your computer and use it in GitHub Desktop.
toggle the status item in the menubar on and off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)awakeFromNib{ | |
//statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; | |
//[statusItem setMenu:statusMenu]; | |
//[statusItem setTitle:@"Status"]; | |
//[statusItem setHighlightMode:YES]; | |
//[[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; | |
{ | |
BOOL checked = NO; | |
if(checked) { | |
BOOL createItem = [self createStatusItem]; | |
if(!createItem) { | |
//Throw an error | |
} | |
} | |
else | |
[self removeStatusItem]; | |
} | |
} | |
- (BOOL)createStatusItem | |
{ | |
NSStatusBar *bar = [NSStatusBar systemStatusBar]; | |
//Replace NSVariableStatusItemLength with NSSquareStatusItemLength if you | |
//want the item to be square | |
statusItem = [bar statusItemWithLength:NSVariableStatusItemLength]; | |
if(!statusItem) | |
return NO; | |
//As noted in the docs, the item must be retained as the receiver does not | |
//retain the item, so otherwise will be deallocated | |
[statusItem retain]; | |
//Set the properties of the item | |
[statusItem setTitle:@"MenuItem"]; | |
[statusItem setHighlightMode:YES]; | |
//If you want a menu to be shown when the user clicks on the item | |
[statusItem setMenu:statusMenu]; //Assuming 'menu' is a pointer to an NSMenu instance | |
return YES; | |
} | |
- (void)removeStatusItem | |
{ | |
NSStatusBar *bar = [NSStatusBar systemStatusBar]; | |
[bar removeStatusItem:statusItem]; | |
[statusItem release]; | |
} | |
- (IBAction)toggleStatusItem:(id)sender | |
{ | |
BOOL checked = [sender state]; | |
if(checked) { | |
BOOL createItem = [self createStatusItem]; | |
if(!createItem) { | |
//Throw an error | |
[sender setState:NO]; | |
} | |
} | |
else | |
[self removeStatusItem]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment