Uiviewcontroller Analysis of iOS development journey

Source: Internet
Author: User

As far as iOS development is concerned, Uiviewcontroller is one of the most core types. And the core idea of iOS's entire UI development is the MVC architecture, and from Uiviewcontroller's name, you can see the role it plays in MVC, and that's the controller.


The controller acts as a director of the entire UI view, making logical processing of the user's input, such as what the user should do when they click on a button, and the view role is only responsible for displaying the views, and this part of the view is the UI we designed in nib or storyboard. Model is our data models, such as entity classes loaded from core data, and so on. This entire architecture is clearly divided, reducing the coupling of the code. The role we have to learn today is controller.

The relationship between Uiviewcontroller and UIWindow and UIView is shown in the following figure:

U Iwindow will set a root controller in real time, then the root view of the root controller will be set to the window's root view,root view and the entire view hierarchy is managed. This builds up the user interface and is less coupled to each other.

According to the official definition, view Controller provides the basic view management model for iOS, that is, it is responsible for managing the view, the level, size, content, interaction and so on. Many native Viewcontroller are available in the iOS SDK to support standard user interfaces such as Table View Controllers (Uitableviewcontroller), navigation Controllers (Uinavigationcontroller), tab bar Controllers (Uitabbarcontroller) and ipad-specific uisplitviewcontroller.


        1, This viewcontroller is primarily used to present content to users and interact with users, such as Uitableviewcontroller,uiviewcontroller.
        2, for other Viewcontroller


Fig. 1 Fig. 2


Uiviewcontroller Load View

Uiviewcontroller has two modes of loading, the first is loading the view by manually loading the Xib file, and the second is loading directly from the code to create the master and apprentice in the view controller. Look directly at the example.

Mode one: Xib loading

When creating a Xib file, set the filename ' owner to the corresponding Uiviewcontroller type, and then associate the file ' owner's view with the root view in Xib ( It is not necessary to create the xib automatically when creating the Uiviewcontroller, it is necessary to create the association manually only if it is created separately, otherwise it will be an error. ), and then create it with the following code:

    mainviewcontroller *vc=[[mainviewcontroller alloc]initwithnibname:@ "Mainviewcontroller" bundle:nil];    Self.window.rootViewController = VC;            Mainviewcontroller *vc=[[mainviewcontroller alloc] init];      


Mode two: Code add view

The idea is to add the view to the root view of Uiviewcontroller, and the default is to load the views from the Xib or storyboard associated with it when Uiviewcontroller starts, and if not found, the root view is nil. We can override the Loadview method by adding a view to it in the form of code. The code is as follows:

Draw when view is loaded-(void) loadview{    uilabel* label = [[UILabel alloc] Initwithframe:[uiscreen mainscreen].applicationframe ] ;    Label.text = @ "Draw the text";    Label.backgroundcolor = [Uicolor redcolor];        self.view = label;}
You can then create the Uiviewcontroller directly from Alloc and init.


The life cycle of Uiviewcontroller

the viewcontroller life cycle experiences initialization, loading views, destroying views, and ending life.

1) Init method

Initializes the viewcontroller itself.

2) Loadview method

when the view needs to be displayed and it is nil, Viewcontroller calls the method, This method needs to be overridden if the code constructs the view.

3) Viewdidload method

After executing the Loadview, continue to execute viewdidload,Loadview when there is no view, and viewdidload when the view has been created.

4) Viewdidunload method

This method is called when the system memory is tight.

5) Dealloc

Frees additional resources or memory.


Uiviewcontroller Analysis of iOS development journey

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.