LoadView, viewDidLoad, and viewDidUnload. How should these three functions be used?
Google, all of them are reprinted. The general content is as follows:
- LoadView manual loading view
- After viewDidLoad is used to load the nib file
- ViewDidUnload is the image of viewDidLoad.
For more information, see the official documentation:
I. loadView
Never take the initiative to call this function. View controller calls this function when the view property is requested and the current view value is nil. If you manually create a view, you should reload this function. If you use IB to create a view and initialize the view controller, it means that you use the initWithNibName: bundle: method. In this case, you should not overload the loadView function.
The default implementation of this method is as follows: First find information about available nib files and load the nib files based on the information. If there is no information about nib files, by default, a blank UIView object is created, and the object becomes the main view of the controller.
So you should do the same when you reload this function. And assign the view of the subclass to the view property (the view you create must be a unique instance and not shared by any other controller ), in addition, the function you overload should not call super.
If you want to further initialize your views, you should do it in the viewDidLoad function. In iOS 3.0 and later versions, you should reload the viewDidUnload function to release any reference to the view or its contents (Child view, etc ).
The materials on the Internet are not comprehensive, especially the blue part.
Ii. viewDidLoad
This function is called after the controller loads related views, regardless of whether these views are stored in the nib file or generated in the loadView function. In most cases, it is the subsequent work of the nib file.
The description of this function is totally incorrect.
3. viewDidUnload
This function is the opposite function of viewDidLoad. When the program memory is insufficient, this function is called by the controller (). Since the controller usually savesView(The view in bold here refers to the view attribute of the controller) related objects (generallyViewSub-view) or references to other objects created at run time, so you must use this function to discard the ownership of these objects for memory reclaim. But do not release the hard-to-recreate data (do not release the data in this functionView).
Generally, the controller will save the reference of views created by the nib file, but it may also save the reference of objects created by the loadView function. The most perfect method is to use the synthesizer method:
1 |
self .myCertainView = nil ; |
In this way, the synthesizer will release the view. If you do not use the property, you have to explicitly release the view yourself.
The description of this function is vague on the Internet. I did not see it after reading it.
In addition, if the controller stores references of other objects and views, you must release these memories in the dealloc method. For iOS2.x, you must also set these references to nil before calling the super dealloc method.
Iv. Conclusion
So the process should be like this:
(LoadView/nib file) to load the view to the memory --> viewDidLoad function to further initialize these views --> when the memory is insufficient, call the viewDidUnload function to release views
--> When you need to use the view, return to the first step.
This cycle