Graphics Programs Reference
In-Depth Information
View Controller Lifecycle
A
UIViewController
begins its life when it is allocated and sent an initializer mes-
sage. A view controller will see its view created, moved on screen, moved off screen, des-
troyed, and created again - perhaps many times over. These events make up the view con-
troller lifecycle.
Initializing view controllers
The designated initializer of
UIViewController
is
initWithNibName:bundle:
.
This method takes two arguments that specify the name of the view controller's XIB file
inside a bundle. (Remember that building a target in
Xcode
creates an application bundle;
this is the same kind of bundle we're talking about here.)
Passing
nil
for both arguments says, “When you load your view, search for a XIB file
whose name is the same as your class and search inside the application bundle.” For ex-
ample,
TimeViewController
will load the
TimeViewController.xib
file. You
can be more explicit about it; add the following code to
TimeViewController.m
.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
self = [super initWithNibName:nil
bundle:nil];
// Get a pointer to the application bundle object
NSBundle *appBundle = [NSBundle mainBundle];
self = [super initWithNibName:@"TimeViewController"
bundle:appBundle];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Time"];
UIImage *i = [UIImage imageNamed:@"Time.png"];
[tbi setImage:i];
}
return self;
}
In practice, iOS programmers use the name of the
UIViewController
subclass as the
name of the XIB file. Thus, when creating view controllers, you need only send
init
.
This is equivalent to passing
nil
for both arguments of
initWithNibName:bundle:
.