Graphics Programs Reference
In-Depth Information
A view controller will destroy its
view
if the system is running low on memory and if its
view
isn't currently on the screen. Run
HypnoTime
in the simulator. Select the
Time
tab
so that both views are loaded.
Now, in the simulator, select
Simulate Memory Warning
from the
Hardware
menu. This
simulates what happens when the operating system is running low on memory and tells
your application to clean up stuff it isn't using.
Switch back to the
Hypnosis
tab; notice that the console reports that
Hyp-
nosisViewController
loaded its
view
again. Now switch to the
Time
tab and no-
tice that
TimeViewController
did
not
reload its view.
TimeViewController
's
view
was on the screen when the memory warning oc-
curred, so its view was not destroyed. (Destroying a view that is on the screen would res-
ult in a miserable user experience.)
HypnosisViewController
's
view
, on the other
hand, was
not
on the screen during the memory warning, so it was destroyed. When you
returned to that view, it was recreated and reloaded.
When the
Hypnosis
tab bar item was tapped, the
UITabBarController
asked the
HypnosisViewController
for its
view
so it could add it as a subview of its own
view
.
HypnosisViewController
, like all view controllers, automatically calls
loadView
if it is sent the message
view
and does not yet have its
view
. The imple-
mentation of
UIViewController
's
view
method looks something like this:
- (UIView *)view
{
if ([self isViewLoaded] == NO)
{
// If I don't currently have a view, then create it
[self loadView];
[self viewDidLoad];
}
// The view is definitely going to exist here, so return it
return view;
}
UIViewController
s that load their view from a XIB file follow the same behavior. In
fact, the default implementation of
loadView
loads the XIB file specified by
initWithNibName:bundle:
. So when view controllers that load their views pro-
grammatically override
loadView
, they intentionally don't call the superclass's imple-
mentation. If they did, it would kick off a search for a XIB file.