Graphics Programs Reference
In-Depth Information
For the More Curious: The main Function and
UIApplication
A C application begins by executing a
main
function. An Objective-C application is no
different, but we haven't seen
main
in any of our iOS applications. Let's take a look now.
Open
main.m
in the
HypnoTime
project navigator. It looks like this:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv,
nil, NSStringFromClass([HypnoAppDelegate class]));
}
}
The function
UIApplicationMain
creates an instance of a class called
UIApplica-
tion
. For every application, there is a single
UIApplication
instance. This object is
responsible for maintaining the run loop. Once the application object is created, its run loop
essentially becomes an infinite loop: the executing thread will never return to
main
.
Another thing the function
UIApplicationMain
does is create an instance of the class
that will serve as the
UIApplication
's
delegate
. Notice that the final argument to
the
UIApplicationMain
function is an
NSString
that is the name of the delegate's
class. So, this function will create an instance of
HypnoAppDelegate
and set it as the
delegate
of the
UIApplication
object. (Where does the
Hypno
in
HypnoAp-
pDelegate
come from? It's what you entered for the class prefix when creating this pro-
ject.)
Right before the run loop begins accepting events, the application sends a message to its
delegate
saying, “Get ready because here we go!” This message is
applica-
tion:didFinishLaunchingWithOptions:
. You implemented this method in
HypnoAppDelegate.m
to create the window and the controller objects used in this ap-
plication.
Every iOS application follows this pattern. If you're still curious, go back and check the
main.m
files in the
Quiz
and
Whereami
applications you wrote earlier.