Reputation: 33036
In an Xxode project that has a lot of .h and .m files, how do you determine which file gets executed first?
Upvotes: 1
Views: 1597
Reputation: 11143
Another thing that should get run first is in an App Delegate (Defined like so: NSObject <UIApplicationDelegate>
and setup in the nib) the method applicationDidFinishLaunching
This is obviously not technically the first thing to be run. The main loop and whatever is in UIApplication will be executed first, but in general this is good starting point for iOS applications and the first place you really have control.
You can also create a class called like "Controller" and drag an NSObject into your nib and set the class to Controller. Then the method awakeFromNib
will be called.
Either of these should be fine to set up your app.
Upvotes: 1
Reputation: 243156
The file that contains int main(int argc, char * argv[]);
will get run first, since the main()
function is the first function to get run. In pretty much every Xcode template project, that file is called "main.m".
edit
Usually, the main()
function of a Cocoa Touch app is this:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
(Substitute NSApplicationMain()
for UIApplicationMain()
and remove the autorelease pool if you're writing a Mac app)
edit #2
I'm only interested in the file that gets run first from the classes folder
The simple answer is "The application delegate", then everything else.
The technical answer to that is that any objects in your MainMenu.xib (Mac) or MainWindow.xib (iOS) file will be instantiated first. Usually the objects in that file will be instantiated in the order in which they appear, but I don't think that's guaranteed.
So if you 3 have custom top-level objects in your MainWindow.xib file, then they'll all be instantiated at the same time (within reason). First their initWithCoder:
methods will execute, then some time later their awakeFromNib
methods will execute (which usually the safest "starting point" for that object).
The application delegate launch methods will happen somewhere along in there too (I believe in between initWithCoder:
and awakeFromNib
).
Upvotes: 9
Reputation: 34185
Cocoa and Cocoa-Touch apps are completely event-driven. It is not that the order the methods are executed can be understood by reading the source code files.
main
function in main.m
. It immediately calls UI/NSApplicationMain
.NS/UIApplicationMain
is a function provided by Cocoa(-Touch). It watches the user interaction, and fires events accordingly.
drawRect:
method you provide: it's called when the system decides to draw an object onto the screen. It's very important that you don't actively draw to the screen. The system asks you to draw the screen, and you respond. applicationDidFinishLaunching:
or the ones which are called when a view is loaded from the xib file, viewDidLoad
.Upvotes: 2