Tuesday, March 1, 2011

How do I execute code on Application startup in a Document-based app

I have a Cocoa document-based application.

When the app launches, I want it to execute some code, which will create a dictionary that I need to be accessible to any document in the application, but I only want the dictionary created when I start the app, not when a new document is opened. Currently I have one controller class, which is instantiated both when the application starts and when new documents are opened.

How do I do this?

From stackoverflow
  • Use this:

    - (void)applicationDidFinishLaunching:(NSNotification *)notification
    {
        //Your code here
    }
    

    in your application delegate.

    It will also work for iPhone.

    Edited according to Peter's comment

    Alex G : I don't think that would work because each time a new document is created, it is making a new instance of my controller class, and would thus not have the dictionary created when the class is instantiated at the App's start
    Time Machine : Add it to your application delegate, or to an object in MainMenu.xib (which is only loaded once)
    Peter Hosey : On the Mac, it should be a notification-handler method: `- (void) applicationDidFinishLaunching:(NSNotification *)notification`

0 comments:

Post a Comment