Menubar App - Is this Right?

I am trying to create an application that, when the main window is not displayed, shows an icon in the menu bar. However, I am not sure about this code and everything is in order. I get no errors and no warnings, but after several applications switch between my application and another, for example. Twitter or Safari I get

Program signal: "EXC_BAD_ACCESS".

Here is my code:

- (void)applicationDidResignActive:(NSNotification*)aNotification
{
        statusItem = [[[NSStatusBar systemStatusBar] 
                       statusItemWithLength:NSVariableStatusItemLength]
                      retain];
        [statusItem setHighlightMode:YES];
        [statusItem setEnabled:YES];

            //Set menubar item tooltip
        [statusItem setToolTip:@"Nucleus"];
        [statusItem setMenu:theMenu];
            //Set the menubar item title
        [statusItem setTitle:[NSString stringWithString:@"N"]]; 

}

- (void)applicationDidBecomeActive:(NSNotification*)aNotification
{
    [statusItem release];
}
+3
source share
2 answers

I just replaced [statusItem release]; with [[NSStatusBar systemStatusBar] removeStatusItem: statusItem]; and it worked. I don't want to release it completely, as I still need the user to switch applications later. Thanks @Kevin Ballard for this! :)

+2

, statusItem, , menu . , , theMenu.

+2

All Articles