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.
Yesterday saw the official Apple related documents and a few related content of the page (123), in fact, this content has been seen before, it seems to have made clear, but still forget. Good acute than bad writing, this time must be well written down.
The premise is that Uiviewcontroller has a uiview. At the same time, two concepts need to be clarified to create a class and instantiate a class. Creating a class and instantiating a class in Xcode is easy to distinguish, but in IB (Interface Builder) Sometimes you get confused. In fact, it is also very good to distinguish, the creation of a nib file alone, there is no other class can be instantiated directly or indirectly, the class or these classes (a nib file I may also contain more than one class) is not an opportunity to be instantiated, so this situation is only through the IB created a class , 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, and then in XOCDE Initwithnibname to instantiate the controller, the controller's initwithcoder will be called.
If your object is a subclass of Uiviewcontroler, then you must call-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle*) Nibbundleornil; method to invoke the nib file to initialize itself, even if that does not use the nib file will also call this function (by default the Init method has done this for you), if you call this method, and pass the two parameters is null (nil), The class then calls-loadview to initialize itself by reading a nib file with the same name as your Uiviewcontroller name. If you do not have such a nib file, you must call-setview: To set a self.view. Or overload the-loadview method.
The basics of iphone development: the use of Loadview/viewdidload/initwithnibname/awakefromnib/initwithcoder