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