Graphics Programs Reference
In-Depth Information
Similarly, if you had a large data structure that you only needed while the view controller
was being displayed, you might want to do some clean-up each time the view controller
was moved off screen. Then you would use viewWillDisappear: or
viewDidDisappear: . Note that these methods, as defined in UIViewController ,
do nothing. They are there so that your subclasses can override them.
- (void) viewWillAppear: (BOOL)animated;
- (void) viewDidAppear: (BOOL)animated;
- (void) viewWillDisappear: (BOOL)animated;
- (void) viewDidDisappear: (BOOL)animated;
Now let's override viewWillAppear: to initialize the time label of the
TimeViewController to the current time and to log to the console. While we're at it,
let's override viewWillDisappear: to log to the console, too. In TimeViewCon-
troller.m , make the following changes:
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"CurrentTimeViewController will appear");
[super viewWillAppear:animated];
[self showCurrentTime:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"CurrentTimeViewController will DISappear");
[super viewWillDisappear:animated];
}
Build and run the application. Note that each time you return to the Time tab, the time la-
bel is updated and a log statement appears in the console. And each time you leave that
screen, you see another log statement in the console.
The most important thing to keep in mind is that the view and its view controller are sep-
arate objects. You can think of the view as a renewable resource that the view controller
uses to communicate with the user. Figure 7.20 shows the life cycle of a view controller's
view in full.
Figure 7.20 Lifecycle of a view controller's view
 
Search WWH ::




Custom Search