[go: up one dir, main page]

0% found this document useful (0 votes)
11 views6 pages

IOS Programming Secret

Uploaded by

autumnfoliage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

IOS Programming Secret

Uploaded by

autumnfoliage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Designing Quiz 设计 Quiz

Designing Quiz 设计 Quiz


You are going to write the Quiz application using the MVC pattern. Here is a breakdown of the 您将使用MVC模式编写<code id='1'>Quiz</code>应用程序。以下是您将创建和操作的实例的分解说
instances you will be creating and working with: 明:

• The model layer will consist of two instances of [String]. • 模型层将包含两个 [字符串] 的实例。

• The view layer will consist of two instances of UILabel and two instances of UIButton. • 视图层将包含两个 UILabel 的实例和两个 UIButton 的实例。

• The controller layer will consist of an instance of ViewController. • 控制器层将包含一个 ViewController 的实例。

These instances and their relationships are laid out in the diagram for Quiz shown in Figure 1.7. 这些实例及其关系在图 1.7 中显示的 Quiz 图中布局。

Figure 1.7 Object diagram for Quiz 图 1.7 Quiz 对象图

Figure 1.7 is the big picture of how the finished Quiz application will work. For example, when the 图 1.7 展示了完成的 Quiz 应用程序的工作概览。例如,当点击 下一步 按钮时,它将触发
Next Question button is tapped, it will trigger a method in ViewController. A method is a lot like a ViewController 中的 方法。方法就像函数一样——是一系列要执行的指令。这个方法将从问题
function – a list of instructions to be executed. This method will retrieve a new question from the array
数组中获取一个新问题,并要求顶部标签显示该问题。
of questions and ask the top label to display that question.
It is OK if this diagram does not make sense yet – it will by the end of the chapter. Refer back to it as 如果这个图暂时看起来没有意义——到本章结束时就会清晰起来。在构建应用程序时,可以参考它,看看它
you build the app to see how it is taking shape. 如何逐渐成型。

You are going to build Quiz in steps, starting with the visual interface for the application. 您将要分步骤构建 Quiz,从应用程序的视觉界面开始。

7 7
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序

Interface Builder 接口构建器


You are using the Single View App template because it is the simplest template that Xcode offers. 您正在使用 单视图应用 模板,因为它是 Xcode 提供的最简单的模板。尽管如此,这个模板
Still, this template has a significant amount of magic in that some critical components have already 包含大量魔法,其中一些关键组件已经为您设置好了。目前,您只需使用这些组件,而无
been set up for you. For now, you will just use these components, without attempting to gain a deep
需深入理解它们的工作原理。本书的其余部分将关注这些细节。
understanding of how they work. The rest of the book will be concerned with those details.
In the project navigator, click once on the Main.storyboard file. Xcode will open its graphical editor 在项目导航器中,单击一次 Main.storyboard 文件。 Xcode 将打开其图形化编辑器,称为 接口构建器(请耐心等
called Interface Builder (be patient; it may take a few moments). You might be asked to give permission 待;可能需要几秒钟)。您可能会被要求授予 SimulatorTrampoline 的权限,它是 Xcode 的一个内部工具。如果
to SimulatorTrampoline, one of Xcode’s internal tools. If you are, grant it. 这样,请授予它。
Interface Builder divides the editor area into two sections: the document outline, on the lefthand side, 接口构建器 将编辑区域分为两个部分:左侧的 文档大纲 和右侧的 画布。
and the canvas, on the right.
This is shown in Figure 1.8. If what you see in your editor area does not match the figure, you may 这在图1.8中显示。如果你的编辑区域中的内容与图不符,你可能需要点击显示文档大纲按钮。
have to click the Show Document Outline button. (If you have additional areas showing, do not worry (如果你有其他显示区域,不用担心它们。)你可能还需要点击文档大纲中的展开三角形来
about them.) You may also have to click the disclosure triangles in the document outline to reveal
显示内容。
content.
Figure 1.8 Interface Builder showing Main.storyboard 图1.8 显示Main.storyboard的接口构建器

8 8
Building the Interface 构建界面

The rectangle that you see in the Interface Builder canvas is called a scene and represents the only 你在接口构建器画布中看到的矩形称为场景,它代表你的应用程序此时拥有的唯一“屏幕”
“screen,” or view, your application has at this time. (Remember that you used the Single View App 或“视图”。(记住你使用单视图应用模板创建了这个项目。)
template to create this project.)
In the next section, you will learn how to create a UI for your application using Interface Builder. 在下一节中,您将学习如何使用 接口构建器 为您的应用程序创建用户界面。接口构建器 允许
Interface Builder lets you drag objects from a library onto the canvas to create instances and also lets 您从库中拖拽对象到画布上以创建实例,并且还允许您在这些对象和您的代码之间建立连接。
you establish connections between those objects and your code. These connections can result in code
这些连接可能导致代码被用户交互调用。
being called by a user interaction.
A crucial feature of Interface Builder is that it is not a graphical representation of code contained in 接口构建器 的一个关键特性是它不是其他文件中包含的代码的图形表示。 接口构建器 是一个对
other files. Interface Builder is an object editor that can create instances of objects and manipulate their 象编辑器,可以创建对象的实例并操作它们的属性。当您完成编辑界面后,它不会生成与您所
properties. When you are done editing an interface, it does not generate code that corresponds to the
做工作对应的代码。.storyboard 文件是一个对象实例的存档,在需要时会被加载到内存中。
work you have done. A .storyboard file is an archive of object instances to be loaded into memory
when necessary.

Building the Interface 构建界面


Let’s get started on your interface. You have selected Main.storyboard to reveal its single scene in the 让我们开始构建您的界面。您已选择 Main.storyboard 以在画布上显示其单个场景。
canvas.
To start, make sure your scene is sized for iPhone 11 Pro. At the bottom of the canvas, find the View 首先,确保您的场景适用于 iPhone 11 Pro。在画布底部,找到 视图 按钮。它可能会显示类似 视图:iPhone
as button. It will likely say something like View as: iPhone 11 Pro (wC hR). (The wC hR will not make 11 Pro (wC hR) 的内容。(wC hR 目前可能没有意义;我们将在第16章中解释它。)如果它已经显示 iPhone 11
sense right now; we will explain it in Chapter 16.) If it says iPhone 11 Pro already, then you are all set. Pro,那么您就设置好了。如果没有,请点击 视图 按钮并找到并选择 iPhone 11 Pro 图标(图1.9)。
If not, click the View as button and find and select the iPhone 11 Pro icon (Figure 1.9).

Figure 1.9 Selecting iPhone 11 Pro 图 1.9 选择 iPhone 11 Pro

It is time to add your view objects to that blank slate. 是时候将您的视图对象添加到那块空白画布上了。

9 9
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序

Creating view objects 创建视图对象


Your application interface requires four view objects: two buttons to accept user input and two text 您的应用程序界面需要四个视图对象:两个按钮用于接受用户输入,两个文本标签用于显示信息。
labels to display information. To add them, first show the library by clicking the button near the top- 要添加它们,首先通过点击 Xcode 顶部右侧附近的按钮(图1.10)来显示库。您也可以使用键盘快
right corner of Xcode (Figure 1.10). You can also open it using the keyboard shortcut Command-Shift- 捷键 Command‑Shift‑L 打开它。(Command‑Option‑Shift‑L 会以单独的窗口打开库。)
L. (Command-Option-Shift-L opens the library in a separate window.)

Figure 1.10 Xcode library 图1.10 Xcode库

10 10
Creating view objects 创建视图对象

The library contains the objects that you can add to a storyboard file to compose your interface. Find 库包含您可以添加到故事板文件中以构建用户界面的对象。通过向下滚动列表或使用库顶部
the Label object by either scrolling down through the list or by using the search bar at the top of the 的搜索栏查找<code id='1'>标签</code>对象。在库中选择此对象并将其拖到画布上的视图
library. Select this object in the library and drag it onto the view object on the canvas. Drag the label
对象上。在画布上拖动标签,注意当标签靠近画布中心时出现的虚线蓝色线条(图1.11)。
around the canvas and notice the dashed blue lines that appear when the label is near the center of the
canvas (Figure 1.11). These guidelines will help you lay out your interface. 这些指南将帮助您布局用户界面。

Figure 1.11 Adding a label to the canvas 图1.11 将标签添加到画布

Using the guidelines, position the label in the horizontal center of the view and near the top, as shown 使用指南,将标签在视图的水平中心位置放置,靠近顶部,如图1.11所示。最终,此标签将向
in Figure 1.11. Eventually, this label will display questions to the user. Reopen the library and drag a 用户显示问题。重新打开库并将第二个标签拖到视图上,并将其放置在水平中心,更靠近中间。
second label onto the view and position it in the horizontal center, closer to the middle. (Tip: If you
(提示:如果您在拖动标签时按住Option键,库将保持打开状态,直到您手动关闭它。)此
hold down Option while dragging the label, the library will then stay open until you close it manually.)
This label will display answers. 标签将显示答案。

Next, find Button in the library and drag two buttons onto the view. Position one below each label. 接下来,在库中找到 按钮,并将两个按钮拖放到视图上。将一个按钮放置在每个标签下方。

11 11
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序

You have now added four view objects to the ViewController’s UI. Notice that they also appear in the 现在,您已向 ViewController 的用户界面添加了四个视图对象。请注意,它们也出现在文档大纲中。您的界面
document outline. Your interface should look like Figure 1.12. 应如图1.12所示。

Figure 1.12 Building the Quiz interface 图1.12 构建测验界面

Configuring view objects 配置视图对象


Now that you have created the view objects, you can configure their attributes. Some attributes of a 现在你已经创建了视图对象,可以配置它们的属性。视图的一些属性,如大小、位置和文本,
view, like size, position, and text, can be changed directly on the canvas. For example, you can resize 可以直接在画布上更改。例如,你可以通过在画布上或文档大纲中选择对象,然后拖动画布
an object by selecting it on the canvas or in the document outline and then dragging its corners and
中的角落和边缘来调整对象的大小。
edges in the canvas.
Begin by renaming the labels and buttons. Double-click each label and replace the text with ???. Then 首先重命名标签和按钮。双击每个标签并将文本替换为<code>???</code>。然后双击上方的按钮并将其名称
double-click the upper button and change its name to Next Question. Rename the lower button to Show 更改为<code>Next Question</code>。将下方的按钮重命名为<code>Show Answer</code>。结果如图
Answer. The results are shown in Figure 1.13. 1.13所示。

12 12

You might also like