Each iOS developer is certainly familiar with Loadview and viewdidload, although the use of these two functions is really very simple, but similar to the initwithnibname/awakefromnib/ It's very confusing to put initwithcoder together.
First, Loadview
Never call this function actively. The view controller calls this function when the property of the view is requested and the current view value is nil. If you create a view manually, you should reload the function. If you create a view with IB and initialize the view controller, that means you use the Initwithnibname:bundle: method, and you should not overload the Loadview function.
The default implementation of this method is to look for information about the available nib files, load the nib file based on this information, and if there is no information about the nib file, the default implementation creates a blank UIView object and then makes the object the controller's main view.
So, when you overload this function, you should do the same. and assign the View property (the view of your create must be a unique instance and not be shared by any other controller), and the function you overload should not call super.
If you want to make further initialization of your views, you should do it in the Viewdidload function. In iOS 3.0 and later, you should reload the Viewdidunload function to free any references to the view or content inside it (sub-view, and so on).
This online information is not comprehensive, especially the blue word part.
Second, Viewdidload
This function is called after the controller has loaded the relevant views, regardless of whether the views are stored in the nib file or generated in the Loadview function. In most cases, it is the follow-up work of nib files.
The description of this function on the Internet is completely wrong.
Third, Viewdidunload
This function is the opposite function of viewdidload. In the absence of program memory, this function is called by the controller to release its view and view-related objects. Since the controller typically holds the view and references to the associated object, you must use this function to discard ownership of these objects for memory recycling. But do not release data that is difficult to reconstruct.
Usually the controller saves a reference to the views created by the nib file, but it may also hold a reference to the object that the Loadview function creates. The perfect method is to use the Synthesizer method:
1 |
self .myCertainView = nil ; |
The synth will release the view, and if you don't use the property, you'll have to show it yourself to release the view.
The description of the function on the net vague, looked at equals not to see.
Iv. Conclusion
So the process should look like this:
(loadview/nib file) to load view into memory-->viewdidload function to further initialize these view--> when memory is low, call the Viewdidunload function to release views
-When you need to use view, go back to the first step
So loop
You need to clarify two concepts, create a class, and instantiate a class. It is easy to distinguish between creating a class and instantiating a class in Xcode, but sometimes it is confusing in the IB (Interface Builder). In fact, it is also very good to distinguish, the creation of a nib file alone, There is no direct or indirect relationship with other classes that can be instantiated, this class or class (a nib file may also contain multiple classes) is not an opportunity to be instantiated, so this is just a class created through IB, Without instantiation. True instantiation also requires reading the nib file by using the code in Xcode. Knowing the difference between the two, these methods are easier to identify.
Viewdidload is actually nothing to be confused about, no matter what path to load (Xcode or IB, where the load is instantiated) after view will definitely execute this method.
Loadview need to be in two different situations. When you instantiate a class through Xcode, you need to implement this method in your controller. It is not necessary to instantiate in IB.
Initwithnibname This method is created in the Controller's class in IB, but is used when instantiating the controller through Xcode.
Awakefromnib This method is called when a class is instantiated in IB. Read the Post found that we recommend the use of Viewdidload instead of using awakefromnib, should be viewdidload will be called multiple times, Awakefromnib is only called once when it is unarchive from the nib file. The actual test found that when the awakefromnib of a class is called, then the viewdidload of the class is not called, which is a strange feeling.
Initwithcoder is called when a class is created in IB but is instantiated in Xocdde. For example, create a controller's nib file with IB, Then the controller is instantiated by Initwithnibname in XOCDE, and the controller's Initwithcoder is called.
Awakefromnib
When the. nib file is loaded, a awakefromnib message is sent to each object in the. nib file, and each object can define its own awakefromnib function to respond to the message and perform some necessary actions. That is, creating a View object from the nib file is performed awakefromnib
Viewdidload
Viewdidload is executed when the view object is loaded into memory, so creating the object either through the nib file or the code will execute Viewdidload
Loadview and Viewdidload and Initwithnibname/awakefromnib/initwithcoder