euphoria83
euphoria83

Reputation: 15136

NSApplicaton delegate - applicationDidFinishLaunching

I have declared a delegate for my cocoa application here :

MyAppDelegate.h

@interface MyAppDelegate : NSApplication {
}
- (void) applicationDidFinishLaunching:(NSNotification*) notice ;
@end

MyAppDelegate.m

@implementation MyAppDelegate 
- (void) applicationDidFinishLaunching:(NSNotification*) notice {
    NSLog(@"inside appdidfinishlaunching") ;
}
@end

I have linked the delegate outlet of File Owner to this object in IB.

Yet, this method is not getting called. I don't see any log messages from it.

Can you please suggest what is wrong ?

Upvotes: 1

Views: 7159

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96323

Your application delegate is not an application itself. It should inherit from NSObject, not NSApplication.

Why that matters

NSApplication is a singleton. Its init method always returns the first instance of NSApplication or any subclass, throwing away any subsequent objects you (or the nib loader) may be calling init on.

So you ended up setting your application object as its own delegate. The object you intended to make the delegate died in the second call to init, and the application object took its place.

Changing the application object to be an instance of your subclass would also have worked, but you'd still have the application as its own delegate, which is unclean and possibly dangerous (NSApplication may privately implement some of its delegate methods itself, as they're just notification handler methods). The only correct solution is to make your app delegate class not inherit from NSApplication.

Upvotes: 7

Related Questions