IOS development-loadview and viewdidload

Source: Internet
Author: User

Every IOS developer must be familiar with loadview and viewdidload. Although these two functions are very simple to use, however, it is very confusing to put it together with similar initwithnibname/awakefromnib/initwithcoder.

I. loadview

Never take the initiative to call this function. Viewcontroller 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 and do not call [Super loadview] During the reload. If you use IB to create a view and initialize viewcontroller, 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:

1. Find information about available nib files and load the nib files according to the information.  // Therefore, the nib loading process is completed in loadview.

2. If there is no information about the NIB file, a blank uiview object is created by default, and the object is assigned to the main view of the viewcontroller.

Therefore, if you decide to reload this function, you should also complete these steps:

Assign the view of the subclass to the view property (the view you create must be a unique instance and is not shared by any other controller ), in addition, the function you overload should not call Super, which is also to maintain a single ing between the main view and the Controller.

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.

This function is mainly used to further initialize your views. Viewdidload is usually responsible for the initialization of the data after the view and its sub-views are loaded into the memory, that is, the initialization of the Data Part of the view. In ios3.0 and later versions, you should overload the viewdidunload function to release any reference to the view or its contents (Child view, etc ).

In most cases, it is the subsequent work of the NIB file.

3. viewdidunload

This function is the opposite function of viewdidload. When the program memory is insufficient, this function is called by the Controller to release its view and view-related objects. Since the Controller usually stores views and references of related objects, you must use this function to discard the ownership of these objects for memory reclaim. But do not release data that is hard to reconstruct.

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
2  =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.

Iv. Conclusion

So the process should be like this:

Loadview to load views (whether nib files or custom views) to memory --> viewdidload function further initializes these views (usually focuses on data Data initialization) --> 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

   Two concepts need to be clarified to create a class and instantiate a class. it is easy to distinguish between creating a class in xcode and instantiating a class, but sometimes it gets confused in IB (interfacebuilder. in fact, it is also very easy to differentiate. When a NIB file is created independently, there is no direct or indirect relationship with other classes that can be instantiated, this class or these classes (a nib file may contain multiple classes) have no chance to be instantiated, so this case only creates a class through IB without instantiation. in actual instantiation, you also need to use code in xcode to read this NIB file. after knowing the differences between the two methods, these methods are much easier to identify.

Viewdidload is actually nothing to confuse. No matter how it is loaded (xcode or IB, loading here is an instantiation), this method will be executed after the view is complete.

Loadview can be divided into two situations. When you instantiate a class through xcode, You need to implement this method in the Controller. In IB instantiation, you do not need to implement this method.

The initwithnibname method is created in the Controller class in IB, but is used when the controller is instantiated through xcode.

Initwithcoder is called when a class is created in IB but instantiated in xocdde. for example, if you create a controller NIB file through IB and then instantiate the controller through initwithnibname in xcode, the Controller's initwithcoder will be called.

Awakefromnib
When. when the NIB file is loaded, an awakefromnib message is sent. every object in the NIB file, each object can define its own awakefromnib function to respond to this message and perform some necessary operations. That is to say, awakefromnib is executed when a view object is created through the NIB file.

Viewdidload
After a view object is loaded into the memory, viewdidload is executed. Therefore, no matter whether you create an object through the NIB file or code, viewdidload is executed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.