Question

My project is document-based, and the code to load a status menu is only executed if I put it under awakeFromNib.

#import "StatusMenuAppDelegate.h"

@implementation StatusMenuAppDelegate

@synthesize window = _window;

-(void)awakeFromNib {
    myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
statusImage = [NSImage imageNamed:@"statusNorm.png"];
[myStatusItem setImage:statusImage];
[myStatusItem setHighlightMode:YES];
[myStatusItem setMenu:myStatusMenu];
}
@end

But, in my test project (non-document-based), the code is executed fine in applicationDidFinishLaunching. Why? Wouldn't I want to ideally use this function for my status menu?

Was it helpful?

Solution

The applicationDidFinishLaunching: message is sent by the application object to its delegate. It is not sent to any other objects.

You can certainly respond to that message in your document objects, but they will never receive it because nothing sends it to them. That's why anything you do in that method in a document object, or any other object that isn't the application's delegate, doesn't happen.

awakeFromNib is one place you could create the status item. I would probably do it in windowControllerDidLoadNib:.

That's assuming you really want to have one status item per document, which probably doesn't make sense. If that's not what you want, you should move creation, ownership, and management of your status item to a singleton object, which should be created—probably within applicationDidFinishLaunching:—by the application's delegate, and have any and all documents interact with that object as needed.

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