The Cocoa scenario happens with a bunch behind the scenes .
1. object viewController
At its most basic, viewController is a common controller object. When it is first allocated initialized, it does not have a view object associated with it. A view is created only when (and if) is required. Thus, without considering the view, the life cycle of the viewController is the same as any other object:
UIViewController * myVC = [[UIViewController alloc] initWith...]; ... [myVC release];
The designated initializer for viewControllers is -initWithNibname:bundle:
If you specify a tip, viewController can automatically load its view from this nib and attach any IBOutlets that you have defined (see below for more details).
2. View loading and unloading
The viewController function will load its view as needed. This usually happens when the -view
method -view
called for the first time and can happen at any time in your program, depending on how you initialize your user interface. The view can also be destroyed and restarted several times during the entire duration of your program, agan, depending on how you manage your user interface. When the viewController determines that its view is required but not yet loaded, the -loadView
method will be called. The main message flow looks something like this:
view loadView viewDidLoad
Note that if you override the -view
method, -loadView
and viewDidLoad
will not be called automatically. If you override -loadView
, you must set the viewController view
property. Otherwise, the next call to -view
will call the boot process again.
You can also unload a view at any time during the life of your program by simply setting the view
property to nil
. The default implementation of -didReceiveMemoryWarning
will automatically default if there is no supervisor in the view (i.e. if it is not currently part of the active view hierarchy). The message flow is as follows:
view = nil viewDidUnload
2a. Software Download Software
If you decide to override -loadView
, you can create a view, subviews, other viewControllers, and any connections between these objects in any way. Of course, this means that you are also responsible for managing memory in relation to the objects being created. If your subclass overrides -loadView
, it must be initialized using nil
for nibName
and bundle
.
2b. Download image from the bottom
If you use a nib file, the default implementation of -loadView
will automatically open that nib file, instantiate its objects, add any connections between them, and take care of your memory management.
Things get a little more complicated with nib files, as this happens behind the scenes. The -awakeFromNib
method -awakeFromNib
called for each object that is created when the nib file is loaded, and there is no guarantee that other objects in the nib file will be fully loaded when it is called.
3. Display views
-viewWillAppear:
-viewDidAppear:
-viewWillDisappear:
and -viewDidDisappear:
only called when the view is displayed or hidden on the screen, especially during animated transitions from one view to another. These methods can be called many times during the life of your program, as the views change places and go out in your navigation scheme.
4. View layout
The -layoutSubviews
method -layoutSubviews
not part of the UIViewController
. It is called for UIView
objects when their boundaries have been changed. If you use a custom subclass of UIView
in your program, you can use this method to create a custom subview layout instead of relying on Cocoa's default autosave methods.
5. Putting it all together
Due to complexity, there are many different ways for this process, but a normal timeline might look something like this:
-[viewController initWithNibname:Bundle:] -[viewController awakeFromNib] -[viewController loadView] -[view awakeFromNib] -[viewController viewDidLoad] -[viewController viewWillAppear] -[viewController viewDidAppear] ... -[viewController viewWillDisappear] // user navigated away -[viewController viewDidDisappear] ... -[viewController viewWillAppear] // user navigated back -[viewController viewDidAppear] ... -[viewController viewWillDisappear] // user navigated away -[viewController viewDidDisappear] ... -[viewController setView:nil] // memory warning, perhaps -[viewController viewDidUnload] ... -[viewController loadView] // user navigated back -[view awakeFromNib] -[viewController viewDidLoad] -[viewController viewWillAppear] -[viewController viewDidAppear] ...