iOS Controller View loading

Source: Internet
Author: User

First we need to know that the controller view is loaded by lazy loading, that is, when it is used to load. What is the role of Loadview, and what is the difference between it and viewdidload. Loadview Method

When we use the Controller view, we call the controller view's Get method, inside the Get method, first determine whether the view has been created, if it already exists, directly return the existing view, if not, then call the controller's Loadview method, Loadview may also be executed multiple times if the controller is not destroyed

Viewdidload Method

When the Loadview method of the controller is executed, the view is created successfully and the Viewdidload method is executed, which, like the Loadview method, can be executed multiple times. In development, we may never have encountered the execution of many times, when will be executed several times?

For example a controller push out of the B controller, at this time, the window shows the B controller's view, at this time if you receive a memory warning, we will generally be a controller in the useless variables and view destroyed, and then when we from the B controller pop to a controller, The Loadview method and the Viewdidload method of the A controller are executed again.

Note that the console prints as shown

load of Controller view

First look at the file structure of the demo, Viewcontroller is a controller, Testviewcontroller is a B controller.

1. Load via Storyboard
When the controller is loaded by storyboard, the name of the storyboard needs to be specified, and the controller view is ultimately what storyboard describes, which is relatively simple and does not elaborate.

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event{  Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "testviewcontroller"  Bundle:nil];   *TESTVC = [Storyboard Instantiateinitialviewcontroller];  [Self.navigationcontroller PUSHVIEWCONTROLLER:TESTVC animated:yes];}

2. Load via Xib

There are three possible scenarios when the controller view is loaded via Xib

A. Specifying the Xib name (otherviewcontroller.xib)

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event{  * TESTVC = [[Testviewcontroller alloc] Initwithnibname:@ "otherviewcontroller"  Bundle: NIL];  [Self.navigationcontroller PUSHVIEWCONTROLLER:TESTVC animated:yes];}

When we specify the name of the Xib, the Loadview method loads the corresponding xib (otherviewcontroller.xib), which is ultimately what it looks like.

B. Xib name not specified 1

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event{  * TESTVC = [[Testviewcontroller alloc] init];  [Self.navigationcontroller PUSHVIEWCONTROLLER:TESTVC animated:yes];}
If we do not specify the Xib name, Loadview will load the Xib (testviewcontroller.xib) with the same name as the controller, which is ultimately the way it is.

C. Do not specify Xib name 2
We will first delete the Testviewcontroller.xib file, this time, we run the program, the result is this.

As we can tell, when the xib name is not specified and there is no xib with the same name as the controller, the Xib (testview.xib) with the same prefix as the controller name is loaded without controllers.

3. Do not load via Sb\xib
Testview.xib This file is also deleted, and then run the program, the result is this.

So black, don't you create a controller view?

For example, the Controller view is present, but the color is clearcolor, so the black is actually uiwindow.

4. Overriding the Loadview method
We rewrite Testviewcontroller's Loadview method, not doing anything inside.

-(void) loadview {}

Run the program to see the results

The result is as black as above, and the difference is that the view is not created this time, note that the outermost layer is not uiview.

If we want the controller view to be loaded out not uiview but other controls, such as Uiimageview, then we can rewrite the Loadview.

- (void)loadView{  self.view = [[UIImageView alloc] init];}

Conclusion

1. Overriding the Loadview method, the view is created based on the overridden Loadview method

2. The controller is loaded via storyboard, then the view is created according to the storyboard description

3. Controller view is loaded via Xib, the view is created according to the xib corresponding to the Nibname

4. Nibname is not specified, the view is created according to Xib with the same name as the controller

5. Without a xib with the same name, create a view based on Xib with the same controller name prefix without controller

6. If none, create a blank xib

Small Details

In the above 2, 32 points in the conclusion, do not know if you have a question?

Why the above is said controller, and the following is said the controller view?
The author unifies the controller awakefromnib method to give everybody to explain this question.
As the name implies, this method is called when the controller is loaded from the nib.

Let's take a look at the situation with storyboard loading

// code in a controller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{  Uistoryboard * Storyboard = [Uistoryboard storyboardwithname:@ "Testviewcontroller" Bundle:nil];  Testviewcontroller *TESTVC = [Storyboard Instantiateinitialviewcontroller];  [Self.navigationcontroller PUSHVIEWCONTROLLER:TESTVC animated:yes];} // The code in the B controller-(void) awakefromnib {  NSLog (@ "B is loaded by nib");}

The console prints "B-loaded through NIB", which calls the Awakefromnib method of the B controller.

Add the previously deleted testviewcontroller.xib file rewrite and see what happens when the xib is loaded

// the code in a controller is changed to the following-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{  Testviewcontroller *TESTVC =[[Testviewcontroller alloc] init];  [Self.navigationcontroller PUSHVIEWCONTROLLER:TESTVC animated:yes];} // the code in the B controller does not change

The console does not have any output, that is, the awakefromnib method of the B controller is not called.

Conclusion

Storyboard loads the controller and controller view, and Xib loads only the controller's view

Last additions

1. Controller View life cycle: Viewdidlayoutsubviews-Viewwilllayoutsubviews, Viewwillappear, viewdidload - > Viewdidappear, viewwilldisappear, Viewdiddisappear

2. Memory warning delivery process: phone memory not enough generate event---Notification application----Invoke application proxy method, pass event to window--window to controller memory warning method

3.xib describes the controller view, its file ' owner is the corresponding controller class name, when through Xib custom view or cell, the file ' s owner cannot be its class name, but can be the controller's class name, in general, the file ' s owner can only be the controller

iOS Controller View loading

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.