Swift's official introductory tutorial series--Two--storyboard and code-interactive translation, source code

Source: Internet
Author: User


Code See github:02_storyboard Interactive code


In this lesson, you will learn how storyboard interacts with your code. When you are done, your application will look like this: learning goals

At the end of the course, you will be able to:

Explain the relationship between scenes in the storyboard and the underlying view controller

Create sockets and action connections between UI elements and source code in the storyboard

Process user input from a text field and display the results in the UI

Make a class conform to a protocol

Understanding the licensing model

When designing the application architecture, follow the target operating mode

Source
connect the UI to the source code

Link to the source code on the element's storyboard. It's important to understand the relationship between the storyboard and the code you're writing.

In the storyboard, a scene represents the content of a screen, usually a view controller. The view controller implements the behavior of your application. The view controller manages a single content view with its child view hierarchy. The view controller coordinates information between the application's mobile data model, which encapsulates the application's data, and displays the view of the data that manages their content, the perception of the view life cycle, the processing direction changes when the device rotates, defines within the navigation of your application, and implements behavior in response to user input. All view Controller object types in iOS Uiviewcontroller or one of its subclasses.

You can define the behavior of the view controller in code by creating and implementing a custom view controller subclass. You can then create a connection between these classes and scenes in the storyboard to get the behavior defined in your code and the user interface that you define in the storyboard.

Xcode has been created and you look ahead, where a class of Viewcontroller.swift, which connects to you now on your storyboard's work site. In the future, when you add more scenarios, you will create this connection yourself in the identity inspector. The identity check allows you to relate the identity of the object to the storyboard, such as what class objects belong to the Edit object's properties.

At run time, your storyboard creates an instance of Viewcontroller, your custom view controller sub-class. The scene from your storyboard appears on the device's screen, and the user interface's behavior is defined Viewcontroller.swift.

Although the site is connected to the Viewcontroller.swift, this is not a unique connection that needs to be made. To define interactions in your application, your view controller source code needs to be able to communicate with the views in the storyboard. You can do this by defining additional connections (called sockets and operations) between the view in the storyboard and the view controller source code files. Create a outlet for a UI element

Outlet provides a way to reference an interface object that you will add to your storyboard from source code files. To create a socket, drag the control from a specific object in the storyboard to the view controller file. This action creates an object in your view controller file, which allows you to access and manipulate the object from the code at run time.

You need to create sockets for text fields and labels in the user interface so that you can reference them.

Connect a text field to the Viewcontroller.swift code

Open your story board, Main.storyboard.

Click the Assistant button in the upper right corner of Xcode near the Xcode toolbar to open the Assistant editor.

If you want to learn more about the workspace, collapse the project navigation and living area by clicking the Xcode button on the navigation and utilities in the toolbar.

You can also collapse outline views.

Select the bar in the editor, which appears at the top of the assistant editor, editing > Changing to Automatic Viewcontroller.swift from the preview assistant.

The Viewcontroller.swift is displayed in the editor on the right.

In Viewcontroller.swift, find the class line, it should be this: Classviewcontroller:uiviewcontroller {

Below the class line, add the following:

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.