In-depth explanation of Uiviewcontroller_ios in IOS development

Source: Internet
Author: User
Tags documentation inheritance

Uiviewcontroller as the name suggests: View Controller. You should play the role of the control layer in the MVC design pattern. Some beginners at the beginning of the time did not understand why there is a uiview to uiviewcontroller what to do, not all to view the increase. Here I would like to state
Uiviewcontroller and UIView are two different classes Uiviewcontroller are view controllers and UIView are views that is to say, Uiviewcontroller controls UIView. You can also think that Uiviewcontroller is a picture frame, and UIView is a photo, the frame can take this photo from time to time for another photo, or add a new photo to the picture. But the photo can't manipulate the frame. UIView works on the frontline, showing the user the content of the presentation and accepting the user's interaction. Uiviewcontroller is equivalent to the director, according to the plan of the UIView in what form to show, which contains some fancy skills, such as flip, fade, and so on. Another function of Uivewcontroller is to handle user interaction, note that I'm talking about "processing", and of course Uiviewcontroller is unaware of user interaction, which requires uiview to interact with the user (for example: touchesbegintouchesmoved) passed up. Two commonly used methods to complete this pass: 1, [self nextresponder] touchesbegin:touches ... 2, use notification in any way, if the Uiviewcontroller get the user input, then it should make some changes to UIView to respond to user input, It should be noted here that Uiviewcontroller should only change the performance of UIView, and should not deal with other things, more operations through the delegate, on the delegate of the application of the next lecture on the way the message is presented together. Please remember: UIView is a view, Uiviewcontroller is a controller, each Viewcontroller manages a view.
If you develop the application interface is very simple, there is no need to control the view with the controller, but for the view of the complex data display and processing, without this controller, this will make the inheritance depth of the code greatly increased, not conducive to the reading of code, First look at the official API for Uiviewcontroller's explanation: The Uiviewcontroller class provides the fundamental view-management model for IPhone Applications (View controller provides the underlying view management model for iphone applications) You can use the instance of Uiviewcontroller to manage a view hierarchy Inheritance of the System Management view). From here, you can see that if you use the view controller, you can easily manage the view of the child view, if there is no such controller, it is conceivable that each view must be inherited to manage each other's relationship.
Uiviewcontroller's basic function is to manage the view in the interface, but a complex application must have a lot of view controllers, so natural if view control has to manage its own function is more powerful. First look at the API documentation: view controllers rarely operate in isolation. If your application uses a navigation or tab bar Controller,or if your application views presents it Modally,then Y has several view controllers interacting with each of the other to implement those navigation (view controllers are rarely used alone. If your application is going to use a navigation or conversion bar controller, perhaps your application will render a modal view, obviously there are many view controllers associated with each other to implement the navigation function. So it can be seen that Uinavigationcontroller and Uitabbarcontroller are used to control the use of view controllers, and their views are also Uiviewcontroller, This shows that Uiviewcontroller can control their own. This is why we are not allowed to push a navigationcontroller into the stack when we use Uinavigationcontroller, so it is very easy to form our own team recursive call itself, resulting in stack overflow. Here are some of the areas where you should pay attention to using Uiviewcontroller.
1. First look at the difference between Loadview and Viewdidload, both of which are used to initialize how the view in the controller is displayed. or first see the official API Explanation: If You create your view manually, you must override the this and use it to create your views. If use Interface Builder to create your views and initialize the view controller that is,you initialize the view using The Initwithnibname:bundle:method,set the Nibname and Nibbundle properties directly, or create both your views and view Controller in Interface Builder-then your must not override this method. (If you create a view controller manually, you must overload this method to use it to create your view.) If you use Interface Builder to create and initialize a view controller, you do not have to overload this method. So when you manually create a view controller, be sure to pay attention to overload Loadview, otherwise your view will not return to show you any additional word view.
2.viewDidUnLoad This method is most misleading, first look at the API explanation: Called when the Controller's view is released from memory (called when the controller's view is released from memory), Personally feel that the official explanation for the English non-native speaker of the country, it is easy to understand when the view controller release, call this method. However, if you actually debug the following, the method is not invoked when the view controller is released. Further analysis of API documentation: this is called as a counterpart to the Viewdidload method.
It is called during-memory conditions when the view controller needs to release its view
And any objects associated with this view to free up memory (this method is invoked relative to the Viewdidload method, in the case of a memory warning, This method is called when an attempt is made to free memory when the controller needs to release its view and any objects associated with it in this view. One more thing to note here, when memory warnings occur, is to invoke the Viewdidunload method of the parent view controller of the view controller that is being displayed, rather than the Viewdidunload method of the view controller being displayed. Because if you call the Viewdidunload method of the view controller that is being displayed, the interface that the user is looking at disappears, and while the memory is freed but apparently unacceptable to the user, it is natural to release the view in the view controller that is not visible below the view. The released view will then call the Viewdidload method the next time it loads, so the Viewdidunload method is corresponding to the Viewdidload method.

Uiviewcontroller life cycle
Uiviewcontrol is an important part of the iOS program, playing the role of a great housekeeper, managing many of the views of the program, today read the official document and do some of the following simple records:

When to load view, what is the principle of the load, when the view disappears, and so on, the documentation is very detailed.

Controller's view is best to load when it needs to be displayed, and releases more view and related data objects than necessary when the system emits a memory warning.

First, the initialization of Uiviewcontroller

Initialization will be based on the need to call Init,initwithcoder and other related functions, this time we can do a simple initialization operation, the establishment of Viewcontroller need to use the data model, It is not recommended to create a view and other display-related objects directly during the initialization phase (which should be created when it is Loadview, or created by lazy loading).

We all know that Viewcontroller can be created in two ways by code and Xib, and that the initialization process is not the same in both ways.

1 VC created by using Xib

Xib is actually going to save our setup as a dataset, when you need to initialize the VC, go back to read the record data set, and then help us create VC dynamically, so you can imagine it in the initialization will first look to see if the implementation of the Initwithcoder method, if the class implements the method, The Initwithcoder method is called directly to create the object, and the Init method is invoked if it is not implemented. The Awakefromnib method is called immediately after the initialization method is called, and in this method we can do further initialization.

2 use code to create VC

When using code creation, we manually create the data in VC as needed, and if we customize VC, we also need to call [Super Init] in init.

Ii. load and unload of view in Uiviewcontroller

Previously, it is not recommended to create a view and other related code when VC initialization, the official document suggests that the initialization of the view should be put on the Loadview, When the VC receives the memory alarm will call didrecievememorywarning this time we have to make a response, release temporarily unwanted objects. If this warning is ignored, the system will continue to send if there is not enough memory, and will force an exit if no processing is available. Below see the specific Loadview and Unloadview time will do what operation.

1) Load Cycle

When the view property needs to be displayed or accessed, the view is not created, and the VC invokes the Loadview method, which creates a view and assigns it to the Vc.view property. Then you will call the VC Viewdidload method, this time Vc.view guarantee is a value, you can do further initialization operations, such as adding some subview. Note: When customizing the VC, you do not need to invoke the [super Loadview] method if you overwrite the Loadview method.

2) Unload cycle

When the app receives a memory warning, it calls each VC's Didrecievememorywarning method, and we need to respond and release resources that are not needed temporarily in the program. This method is usually overridden, which requires that the method of super be invoked when overridden. If you detect the current VC view can be safely released, it will call the Viewwillunload method, which we must pay attention to, because when the VC view disappears when its subviews may be released together, we need to make some records according to the circumstances, To ensure that the next time it is created correctly, there is no memory leak. When Viewwillunload is invoked, the Vc.view property is set to nil, and then the Viewdidunload method is invoked, at which point we can release those strongly referenced objects.

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.