tags:

views:

2316

answers:

2

Hi All,

I have an application where I have a main.m that returns NSApplicationMain(argc, (const char **) argv);.

I want to run code on -applicationDidFinishLaunching:, but I just dont see how to do it.

Can anyone help or have an example?

Thanks! -Jason

+8  A: 

The applicationDidFinishLaunching: method of the NSApplication delegate will be called when the app has finished loading. Many of the project templates setup a delegate. If you are using one just add the appropriate method to it.

If your project does not have an app delegate set up you will need to do that yourself. First, make a new class to act as your delegate (you can use an exiting one if there is something logically appropriate). Now make sure that class is instantiated in your MainMenu.nib. Finally, hook the delegate property of the "File's Owner" object to the instantiate delegate in IB.

Louis Gerbarg
ahh cocoa is so intuitive :D
Shawn Simon
If for whatever reason you aren't using a nib (and if not, why not?) you can set up the app delegate instance yourself in main.m. You'll have to stop using NSApplicationMain() and instead handle some stuff yourself and then call [[NSApplication sharedApplication] run];
Kevin Ballard
+1  A: 

Louis' answer is concise and spot-on. However, if the concept of delegate methods is new to you, you'd do well to check out the relevent documenation.

zbrimhall