tags:

views:

316

answers:

3

I want to load some data from mysql into my cocoa application view before the application starts.

I am sure that this should happen in the controller so that it can send the required data to the view.

I am looking for a method or common technique that is used for this sort of thing.

Many Thanks

+2  A: 

Sounds like you're looking for the awakeFromNib function.

http://www.cocoadev.com/index.pl?AwakeFromNib

DannySmurf
+1  A: 

You can use the - applicationDidFinishLaunching: or - applicationWillFinishLaunching: delegate messages, by implementing one of them in your application delegate/controller, and do whatever initialization you want there.

Nathan Kinsinger
+1  A: 

Cocoa gives you many places to perform tasks before and after objects are loaded from a nib, but it's important to read the documentation carefully to make sure things are happening in the order you expect. Usually I use the following strategy when I'm working on a Cocoa application:

  • Where appropriate I implement the +(void)initialize method, which is called before any instances of a class are created. I'll probably set the app's default preferences here, for example.
  • In my application controller (app delegate), I implement the applicationDidFinishLaunching: delegate method to load my data file. If this works okay, I then create the window controller(s) and display any windows I want to show at launch.
  • In the window/view controllers, I override windowDidLoad: or loadView to perform tasks involving objects loaded from a nib. If I need to create any instance variables that don't involve the nib, I also override the init method and do that there.
  • If I need to do anything in my view objects after they're loaded from a nib, I'll override awakeFromNib.
Marc Charbonneau