VI, the protagonist of app development--uiviewcontroller

Source: Internet
Author: User

MVC Framework Pattern

MVC, the model, view, and Controller, is a software design pattern designed for software that contains a graphical user interface that has been in its history for more than more than 30 years since the 1980s. Although years old, but the years of this kill the pig knife does not seem to kill much of the vitality of MVC, today, MVC is still active in the world's major frameworks, including Qt, MFC, C#& NET and so on, and on the basis of it, also derived from "Hierarchical MVC" (HMVC), "Model-view-adapter" (MVA) and other rookie models.

MVC specifically refers to: The graphical software is divided into model, view and controller a total of 3 parts:

Model: Responsible for data dynamic management and software business logic, receive the operation from controller, and pass the result to view in time.

View: Information that is directly present in front of the user, such as a picture or a piece of text.

Controller: Receives input from the user and converts it into the corresponding command to the model.

The advantage of MVC is that 3 parts have their own responsibilities, clear responsibility and low coupling, greatly reducing software complexity, thus improving the quality of software and development efficiency.

When we're using AutoLayout to lay out our UI controls, we're already using the MVC design approach. Uiviewcontroller, our protagonist today, is the controller in MVC. Don't talk much, let's start with a simple uiviewcontroller.

To Android developers: Uiviewcontroller to iOS is like activity on Android.

Response button

First, we re-created the 1 project "Create a new Xcode project", "single View App", named Project "Charpter6"

The new project creates 1 Uiviewcontroller by default and is named Viewcontroller (for example), and we see that Viewcontroller generates 2 functions by default that we need to refine.

Where didreceivememorywarning is used to handle memory warnings in case of memory leaks. We will introduce them in subsequent chapters, and we will not be able to remove them or ignore them at this time.

Another function, Viewdidload, is the focus of this section, as the name implies, which is used to load the view.

We switch to the secondary editing interface and open Main.storyboard, then drag a button to the storyboard viewcontroller panel.

We then add the appropriate constraint to the button (left/top/right margin 0, height 50)

Next we'll have the controller and the button connected so that the controller can reference, modify, and respond to the button in code.

The first is to create a reference to the button in Viewcontroller:

Use "Ctrl-Drag" (Press and hold the Ctrl key and drag the left mouse button) on the storyboard button into the code.

The reference to the UI control on storyboard is called "Outlet", which is the option for connection.

Type Select Uibutton,storage Select Weak,name can be arbitrarily specified, here we name it "button", then click Connect.

At this point we see that Viewcontroller has 1 more member variables: button, and this is the reference to the button on storyboard, so that we can modify the button by code.

Then there is the click event that lets Viewcontroller get the button:

Use the button on the "Ctrl-Drag" storyboard. The Click event of the UI control on storyboard is called "Action", which is the option for connection.

Type Select Uibutton,event Select Touch Up inside,storage Select Weak,name can be arbitrarily specified, here we name it as "Buttonaction", then click Connect.

At this point we see that Viewcontroller has 1 more member functions: Buttonaction, and this is the Click event Response function for the button on the storyboard, so that we can respond to the button click through the code.

Now we want the button to change its content after the click, the "button" is changed to "I rookie to this trip", so we add 1 lines in the function buttonaction:

button. Settitle ("I rookie to this trip", for:. Normal)

Previous section headings recording next section

VI, the protagonist of app development--uiviewcontroller

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.