Graphics Programs Reference
In-Depth Information
In TimeViewController.h , declare that timeLabel is a weak reference.
@interface TimeViewController : UIViewController
{
__weak IBOutlet UILabel *timeLabel;
}
Then, change viewDidUnload in TimeViewController.m so that it confirms that
timeLabel is nil as soon as the view is unloaded.
- (void)viewDidUnload
{
[super viewDidUnload];
NSLog(@"Unloading TimeViewController's subviews");
timeLabel = nil;
NSLog(@"timeLabel = %@", timeLabel);
}
Build and run the application. Make TimeViewController unload its view by simu-
lating a memory warning while its view is not visible. Notice that timeLabel is nil by
the time viewDidUnload runs.
The situation of a view controller having two references to a view is common. To avoid
memory leaks, the convention for IBOutlet s is to declare them as weak references.
There is one exception to this rule: you must keep a strong reference to top-level objects
in a XIB file. A top-level object in a XIB file sits in the top-level of the of the Objects sec-
tion in the outline view; you don't have to click the disclosure tab next to another object to
see it. For example, a view controller's view is a top-level object, but its subviews are
not ( Figure 7.19 ).
Figure 7.19 XIB top level objects
The view controller, then, has ownership of its view , which owns all of its subviews .
All of the objects continue to exist as long as you need them to. Therefore, you do not
 
 
Search WWH ::




Custom Search