Example of how to know when program initialisation (initialization) is complete

14: How to know when initialisation (initialization) is complete


We want to know when it is safe to talk to any of the other objects that may have woken up from nib. The solution is to use the fact that the application sends out the notification applicationDidFinishLaunching to its delegates once it has completed initialisation and just before the start of the program loop.

Therefore define the method (void)applicationDidFinishLaunching:(NSNotification *)aNotification in one of one's objects and make that object an application delegate.


Create a new XCode project. Call it: 014_applicationDidFinishLaunching.

Create program file MyView of type NSView and code it as follows:

// MyView.h // 014_applicationDidFinishLaunching // #import <Cocoa/Cocoa.h> @MyView : NSView { } @end // MyView.m // 014_applicationDidFinishLaunching // #import "MyView.h" @implementation MyView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // In Interface Builder click on the Application icon then drag from its Outlets->delegate entry // to the MyControl icon and there it is done, this method will be called. Yes. Really! NSLog(@"MyView applicationDidFinishLaunching"); } // end applicationDidFinishLaunching - (void)drawRect:(NSRect)pRect { [[NSColor blueColor] set]; NSRectFill( pRect ); // draw a couple of letters, just for the craic // see p282 of the Hillegass: Cocoa Programming 2nd ed. NSMutableDictionary *zDictAttributes = [[NSMutableDictionary alloc] init]; [zDictAttributes setObject:[NSFont fontWithName:@"Helvetica" size:60] forKey:NSFontAttributeName]; [zDictAttributes setObject:[NSColor yellowColor] forKey:NSForegroundColorAttributeName]; NSPoint zPoint; zPoint.x = 10.0; zPoint.y = 10.0; NSString * zString = @"Wake \nUp!!!"; [zString drawAtPoint:zPoint withAttributes:zDictAttributes]; } @end

Bring up Interface Builder by double clicking on MainMenu.nib.

Drag a Custom View onto the window and subclass it to MyView in the Inspector.

Save and Run. Nothing will display in in the Conslole log, i.e. MyView has not received the applicationDidFinishLaunching message.

Go back into Interface Builder and click on the Application icon. In the connections panel of the Inspector, drag from the delegates tab to the custom view.

Make the NSView a delegate of NSApplication

Make the NSView a delegate of NSApplication

Save and run. This time the message will display in the Console log.

The applicationDidFinishLaunching message

The applicationDidFinishLaunching message

If you want to download the code

Click the Download Link to obtain 012-Show-Panel.zip file of this whole OS X 10.5 Leopard program.

Download 014_applicationDidFinishLaunching.zip (2.3 MB)

Useful Links

Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • Link to picture of flying spaghetti monster with pirates attacking tower of babel