Analysis of UIViewController in iOS development

Source: Internet
Author: User

Analysis of UIViewController in iOS development

For iOS development, UIViewController is one of the core types. The core idea of iOS UI development is also the MVC Architecture. From the name of UIViewController, we can see that the role it plays in MVC is Controller.


As the Controller of the UI View, the Controller performs logical processing on user input, such as what operations should be performed when a user clicks a button. The View role is only responsible for displaying views, this part of view is the UI we designed in nib or storyboard. Model is our data Model, such as the entity class loaded from Core data. The entire architecture has a clear division of labor, reducing the code coupling. The role we want to learn today is Controller.

The relationship between UIViewController and UIWindow and UIView is shown in the figure below:

Vc + Principal/Principal + PHA + ICAgICAgICCwtL3hubm/Principal + PHA + Principal + 08PT2r/Y1sa6zc/Principal + Principal/Principal + HP1Mq + yc/release + release/wLi1xMrTzby/2 NbGxvejrLXXsr/AuLC0xaW1xLj2yv2 + release = "http://www.2cto.com/uploadfile/Collfiles/20141202/20141202081454107.png" alt = "\" width = "300" height = "534"/>

Figure 1 Figure 2


UIViewController load View

UIViewController can be loaded in two ways. The first is to manually load the xib file to load the View, and the second is to directly create the mentor in the View Controller through code. Let's look at the example.

Method 1: xib Load

Create an xib File and set the File's Owner to the corresponding UIViewController type, then associate the view of the File's Owner with the root view in the xib (this step is not required if xib is automatically created when the UIViewController is created. You need to manually establish the association only when creating the UIViewController, otherwise, an error is reported .), Then you can create an instance by using the following code:

//    MainViewController *vc=[[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];  //    self.window.rootViewController = vc;            MainViewController *vc=[[MainViewController alloc] init];      self.window.rootViewController = vc; 


Method 2: add View by code

The idea is to add the View to the root view of UIViewController. When UIViewController is started, the view is loaded from the associated xib or storyboard by default. If no View is found, the root view is nil. We can override the loadView method and add a view to it in the form of code. The Code is as follows:

 
// Draw when loading the view-(void) loadView {UILabel * label = [[UILabel alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame]; label. text = @ "Draw The Text"; label. backgroundColor = [UIColor redColor]; self. view = label ;}
Then you can directly create UIVIewController through alloc and init.


UIViewController Lifecycle

The life cycle of ViewController is initialized, loaded, destroyed, and ended.

1) init Method

Initialize ViewController itself.

2) loadView Method

When the view needs to be displayed but it is nil, viewController calls this method. If the code constructs the View, it needs to override this method.

3) viewDidLoad Method

After loadView is executed, viewDidLoad is executed. No view is available at loadView, and view is created at viewDidLoad.

4) viewDidUnload Method

This method is called when the system memory is tight.

5) dealloc

Release other resources or memory.


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.