Making connections 建立连接
Making connections 建立连接
A connection lets one object know where another object is in memory so that the two objects can 一个 连接 让一个对象知道另一个对象在内存中的位置,以便两个对象可以通信。在 接口构
communicate. There are two kinds of connections that you can make in Interface Builder: outlets and 建器 中,你可以创建两种连接:出口和动作。一个 出口 是一个对象的引用。一个 动作 是一
actions. An outlet is a reference to an object. An action is a method that gets triggered by a button or
个由按钮或用户可以交互的其他视图(例如滑块或选择器)触发的方法。
some other view that the user can interact with, like a slider or a picker.
Let’s start by creating outlets that reference the instances of UILabel. Time to leave Interface Builder 让我们从创建引用 UILabel 实例的出口开始。是时候离开 接口构建器 并写一些代码了。
and write some code.
Declaring outlets 声明出口
In the project navigator, find and select the file named ViewController.swift. The editor area will 在项目导航器中,找到并选择名为 ViewController.swift 的文件。编辑区域将从 接口构建器 切换到 Xcode
change from Interface Builder to Xcode’s code editor. 的代码编辑器。
In ViewController.swift, start by deleting any code that the template added between class 在 ViewController.swift 中,首先删除模板在 classViewController:
ViewController: UIViewController { and the final brace, so that the file looks like this: UIViewController { 和最后一个大括号之间添加的任何代码,以便文件看起来像这样:
import UIKit import UIKit
class ViewController: UIViewController { class ViewController: UIViewController {
} }
(For simplicity, we will not show the line import UIKit again for this file.) (为简化起见,对于此文件,我们将不再显示行 import UIKit。)
Next, add the following code that declares two properties. (Throughout this book, new code for you 接下来,添加以下声明两个属性的代码。(在本书中,您要添加的新代码将以粗体显示。
to add will be shown in bold. Code for you to delete will be struck through.) Do not worry about 您要删除的代码将被划线。)现在不用担心理解代码或属性;先添加进去。
understanding the code or properties right now; just get it in.
class ViewController: UIViewController { class ViewController: UIViewController
@IBOutlet var questionLabel: UILabel! {@IBOutlet var 问题标签: UILabel! @IBOutlet
@IBOutlet var answerLabel: UILabel!
var 答案: UILabel!}
}
This code gives every instance of ViewController an outlet named questionLabel and an outlet 这段代码为每个 ViewController 实例提供一个名为 questionLabel 的出口和一个名为
named answerLabel. The view controller can use each outlet to reference a particular UILabel object answerLabel 的出口。视图控制器可以使用每个出口来引用特定的 UILabel 对象(即,您视图中的
(that is, one of the labels in your view). The @IBOutlet keyword tells Xcode that you will connect 标签之一)。@IBOutlet 关键字告诉 Xcode 您将使用 Interface Builder 将这些出口连接到标签对象。
these outlets to label objects using Interface Builder.
19 19
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Setting outlets 设置出口
In the project navigator, select Main.storyboard to reopen Interface Builder. 在项目导航器中,选择 Main.storyboard 以重新打开 Interface Builder。
You want the questionLabel outlet to point to the instance of UILabel at the top of the UI. 您希望 问题标签 出口指向 UI 顶部的 UILabel 实例。
In the document outline, find the View Controller Scene section and the View Controller object within 在文档大纲中,找到 视图控制器场景 部分,以及其中的 视图控制器 对象。在这种情况下,视
it. In this case, the View Controller stands in for an instance of ViewController, which is the object 图控制器 代表一个 ViewController 实例,该实例负责管理在 Main.storyboard 中定义的界面。
responsible for managing the interface defined in Main.storyboard.
Control-drag (or right-click and drag) from the View Controller in the document outline to the top label 从文档大纲中的 视图控制器 控制拖动(或右键点击并拖动)到场景中的顶部标签。当标签高亮
in the scene. When the label is highlighted, release the mouse and keyboard; a black panel will appear, 时,释放鼠标和键盘;将出现一个黑色面板,如图1.22所示。选择 问题标签 来设置出口。
as shown in Figure 1.22. Select questionLabel to set the outlet.
Figure 1.22 Setting questionLabel 图1.22 设置问题标签
(If you do not see questionLabel in the connections panel, double-check your ViewController.swift (如果在连接面板中看不到 问题标签,请检查您的 ViewController.swift 文件是否有拼写错误。)
file for typos.)
Now, when the storyboard file is loaded, the ViewController’s questionLabel outlet will 现在,当故事板文件加载时,ViewController 的 问题标签 出口将自动引用屏幕顶部的
automatically reference the instance of UILabel at the top of the screen, which will allow the UILabel 实例,这将允许 ViewController 告知标签要显示的问题。
ViewController to tell the label what question to display.
Set the answerLabel outlet the same way: Control-drag from the ViewController to the bottom UILabel 以相同的方式设置 answerLabel 出口:从 ViewController 控制拖动到底部 UILabel 并选择 answerLabel(图
and select answerLabel (Figure 1.23). 1.23)。
20 20
Making connections 建立连接
Figure 1.23 Setting answerLabel 图1.23 设置answerLabel
Notice that you drag from the object with the outlet that you want to set to the object that you want that 请注意,您需要从具有您要设置的出口的对象拖动 到 您希望该出口指向的对象。
outlet to point to.
Your outlets are all set. The next connections you need to make involve the two buttons. 您的出口都已设置。下一步需要建立的连接涉及两个按钮。
Defining action methods 定义动作方法
When a UIButton is tapped, it calls a method on another object. That object is called the target. The 当一个 UIButton 被点击时,它会在另一个对象上调用一个方法。该对象称为 目标。被触发
method that is triggered is called the action, and it contains the code to be executed in response to the 的该方法称为 动作,其中包含在按钮被点击时执行的代码。
button being tapped.
In your application, the target for both buttons will be the instance of ViewController. Each button 在您的应用程序中,两个按钮的目标将是 ViewController 的实例。每个按钮将有自己的动
will have its own action. Let’s start by defining the two action methods: showNextQuestion(_:) and 作。让我们从定义两个动作方法开始:showNextQuestion(_:) 和 showAnswer(_:)。
showAnswer(_:).
Reopen ViewController.swift and add the two action methods after the outlets. 重新打开 ViewController.swift 并在出口之后添加两个动作方法。
class ViewController: UIViewController { 类 ViewController: UIViewController { @IBOutlet
@IBOutlet var questionLabel: UILabel! var 问题标签: UILabel! @IBOutlet var 答案: UILabel!
@IBOutlet var answerLabel: UILabel!
@IBAction func showNextQuestion(_ sender: UIButton) { @IBAction func 显示下一步问题(_ sender: UIButton) {
} }
@IBAction func showAnswer(_ sender: UIButton) { @IBAction func showAnswer(_ sender: UIButton) {
} }
} }
You will flesh out these methods after you make the target and action connections. The @IBAction 在您完成目标与动作的连接后,您将完善这些方法。@IBAction关键字告诉Xcode,您将在接口构建器中完
keyword tells Xcode that you will be making these connections in Interface Builder. 成这些连接。
21 21
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Setting targets and actions 设置目标和动作
Switch back to Main.storyboard. Let’s start with the Next Question button. You want its target to be 切换回Main.storyboard。让我们从下一步按钮开始。您希望其目标为ViewController,动作
ViewController and its action to be showNextQuestion(_:). 为showNextQuestion(_:)。
To set an object’s target, you Control-drag from the object to its target. When you release the mouse, 要设置对象的<code>目标</code>,请<code>控制拖动</code> 从对象<code>到</code>其<code>目标</code>。当您
the target is set, and a panel appears that lets you select an action. 释放鼠标时,<code>目标</code>将被设置,并出现一个面板,让您选择一个<code>动作</code>。
Select the Next Question button on the canvas and Control-drag to the View Controller in the 选择画布上的<code>下一步问题</code>按钮,并<code>控制拖动</code>到文档大纲中的<code>视
document outline. When the View Controller is highlighted, release the mouse button and choose 图控制器</code>。当<code>视图控制器</code>被高亮显示时,释放鼠标按钮,并在连接面板中的
showNextQuestion: under Sent Events in the connections panel, shown in Figure 1.24. <code>已发送事件</code>下选择<code>showNextQuestion:</code>,如图1.24所示。
Figure 1.24 Setting Next Question target and action 图1.24 设置下一步问题目标和动作
Now for the Show Answer button. Select the button and Control-drag from the button to the View 现在来处理 显示答案 按钮。选择该按钮,然后从按钮处控制拖动到 视图控制器。从连接面板中选择
Controller. Choose showAnswer: from the connections panel. showAnswer:。
22 22
Making connections 建立连接
Summary of connections 连接摘要
There are now five connections between the ViewController and the view objects. You have set the 现在在 ViewController 和视图对象之间有五个连接。您已将属性 answerLabel 和
properties answerLabel and questionLabel to reference the label objects – two connections. The questionLabel 设置为引用标签对象——两个连接。ViewController 是两个按钮的目标——另
ViewController is the target for both buttons – two more. The project’s template made one additional 外两个。项目的模板又创建了一个额外的连接:ViewController 的 view 属性连接到了代表应
connection: The view property of ViewController is connected to the View object that represents the 用程序背景的 View 对象。这样一共有五个。
background of the application. That makes five.
You can check these connections in the connections inspector. First, make sure that the inspector area 您可以在 连接检查器 中检查这些连接。首先,请确保 检查器区域 在 Xcode 的窗口中可见
within Xcode’s window is visible (Figure 1.25). You may need to click the rightmost button of the (图 1.25)。您可能需要点击窗口右上角控件中最右侧的按钮。检查器区域位于编辑区域右
control in the top-right corner of the window. The inspector area is to the right of the editor
侧,其中包含各种检查器,用于显示编辑区域中选择的文件或对象的设置。
area and contains the various inspectors that display settings for a file or object selected in the editor
area.
Figure 1.25 Xcode inspector area 图 1.25 Xcode 检查器区域
23 23
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Next, select the View Controller in the document outline. Then, in the inspector area, click the tab to 接下来,在文档大纲中选择 视图控制器。然后,在检查器区域中,点击标签以显示连接检查器(图 1.26)。
reveal the connections inspector (Figure 1.26).
Figure 1.26 Checking connections in the connections inspector 图1.26 在连接检查器中检查连接
Your storyboard file is complete. The view objects have been created and configured and all the 您的Storyboard文件已完成。视图对象已创建和配置,并且所有必要的连接都已连接
necessary connections have been made to the controller object. Let’s move on to creating and 到控制器对象。让我们继续创建和连接您的模型对象。
connecting your model objects.
24 24