Find Out How an App Starts Up
Because you based
your project on an Xcode template, much of the basic app environment is
automatically set up when you run the app. For example, Xcode creates an
application object which, among a few other things, establishes the run
loop (a run loop registers input sources and enables the delivery of
input events to your app). Most of this work is done by the
UIApplicationMain
function, which is supplied for you by the UIKit framework and is automatically called in your project’s main.m
source file.
The
main
function in main.m
calls the UIApplicationMain
function within an autorelease pool:@autoreleasepool {
|
return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloWorldAppDelegate class]));
|
}
|
The
@autoreleasepool
statement supports the Automatic Reference Counting (ARC) system. ARC
provides automatic object-lifetime management for your app, ensuring
that objects remain in existence for as long as they're needed and no
longer.
The call to
UIApplicationMain
creates an instance of the UIApplication
class and an instance of the app delegate (in this tutorial, the app delegate is HelloWorldAppDelegate
, which is provided for you by the Single View template). The main job of the app delegate
is to provide the window into which your app’s content is drawn. The
app delegate can also perform some app configuration tasks before the
app is displayed. (Delegation is a design pattern in which one object acts on behalf of, or in coordination with, another object.)
In an iOS app, a window
object provides a container for the app’s visible content, helps
deliver events to app objects, and helps the app respond to changes in
the device’s orientation. The window itself is invisible.
The call to
UIApplicationMain
also scans the app’s Info.plist
file. The Info.plist
file is a property list (that is, a structured list of key-value pairs)
that contains information about the app such as its name and icon.
Because you chose to use a storyboard in this project, the
Info.plist
file also contains the name of the storyboard file that the application object should load. A storyboard contains an archive of the objects, transitions, and connections that define an app’s user interface.
In the HelloWorld app, the storyboard file is named
MainStoryboard.storyboard
(note that the Info.plist
file shows only the first part of this name). When the app starts, MainStoryboard.storyboard
is loaded and the initial view controller is instantiated from it. A view controller is an object that manages an area of content; the initial view controller is simply the first view controller that gets loaded when an app starts.
The HelloWorld app contains only one view controller (specifically,
HelloWorldViewController
). Right now, HelloWorldViewController
manages an area of content that is provided by a single view. A view
is an object that draws content in a rectangular area of the screen and
handles events caused by the user’s touches. A view can also contain
other views, which are called subviews. When you add a subview to a view, the containing view is called the parent view and its subview is called a child view. The parent view, its child views (and their child views, if any) form a view hierarchy. A view controller manages a single view hierarchy.
In a later step, you’ll create a view hierarchy by adding three subviews to the view that’s managed by
HelloWorldViewController
; these three subviews represent the text field, the label, and the button.
You can see visual representations of the view controller and its view in the storyboard.
No comments:
Post a Comment