1 1
A Simple iOS Application 一个简单的iOS应用程序
In this chapter, you are going to write an iOS application named Quiz (Figure 1.1). This application 在本章中,你将要编写一个名为 Quiz(图1.1)的iOS应用程序。该应用程序将显示一个问
will show a question and then reveal the answer when the user taps a button. Tapping another button 题,当用户点击按钮时,会显示答案。点击另一个按钮将向用户显示一个新问题。
will show the user a new question.
Figure 1.1 Your first application: Quiz 图1.1 您的第一个应用程序:Quiz
When you are writing an iOS application, you must answer two basic questions: 当您正在编写iOS应用程序时,您必须回答两个基本问题:
• How do I get my objects created and configured properly? (Example: “I want a button here that • 我如何让我的对象正确创建和配置?(示例:“我想在这里有一个按钮,上面写着 下一步问题。”)
says Next Question.”)
• How do I make my app respond to user interaction? (Example: “When the user taps the button, I • 我如何让我的应用程序响应用户交互?(示例:“当用户点击按钮时,我想执行这段代码。”)
want this piece of code to be executed.”)
Most of this book is dedicated to answering these questions. 本书的大部分内容都是用来回答这些问题。
1 1
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
As you go through this first chapter, you will probably not understand everything that you are doing, 在阅读这一章时,你可能不会理解你所做的一切,并且可能会觉得只是在走形式。但走形式
and you may feel ridiculous just going through the motions. But going through the motions is enough 现在就足够了。模仿是一种强大的学习形式;这是你学会说话的方式,也是你开始 iOS 编程
for now. Mimicry is a powerful form of learning; it is how you learned to speak, and it is how you will
的方式。随着你能力的提升,你将尝试挑战自己在平台上进行创造性的事情。现在,请继续
start iOS programming. As you become more capable, you will experiment and challenge yourself to
do creative things on the platform. For now, go ahead and do what we show you. The details will be 做我们展示给你的。细节将在后面的章节中解释。
explained in later chapters.
Creating an Xcode Project 创建Xcode项目
Open Xcode and, from the File menu, select New → Project.... (If Xcode opens to a welcome screen, 打开 Xcode,然后从 文件 菜单中选择 新建 → 项目...。(如果 Xcode 打开到欢迎界面,请选择 创建新的
select Create a new Xcode project.) Xcode项目。)
A new workspace window will appear and a sheet will slide down from its toolbar. At the top, find the 将出现一个新的工作区窗口,并且一个表单将从其工具栏滑下。在顶部,找到iOS部分,然后
iOS section and then the Application area (Figure 1.2). You are offered several application templates to 是应用程序区域(图1.2)。您可以选择几个应用程序模板。选择单视图应用。
choose from. Select Single View App.
Figure 1.2 Creating a project 图1.2 创建项目
This book was created for Xcode 11.4. The names of these templates may change with new Xcode 本书是为 Xcode 11.4 编写的。这些模板的名称可能会随着新的 Xcode 版本而变化。如果你看不
releases. If you do not see a Single View App template, use the simplest-sounding template. You can 到 单视图应用 模板,请使用听起来最简单的模板。你也可以访问本书的 Big Nerd Ranch 论
also visit the Big Nerd Ranch forum for this book at forums.bignerdranch.com for help working with
坛 forums.bignerdranch.com,以获取有关使用新版 Xcode 的帮助。
newer versions of Xcode.
2 2
Creating an Xcode Project 创建Xcode项目
Click Next and, in the next sheet, enter Quiz for the Product Name (Figure 1.3). The organization name 点击下一步,在下一个表单中,为产品名称输入Quiz(图1.3)。要继续,需要组织名称和标识符。您可以
and identifier are required to continue. You can use Big Nerd Ranch or any organization name you 使用Big Nerd Ranch或任何您喜欢的组织名称。对于组织标识符,您可以使用com.bignerdranch或
would like. For the organization identifier, you can use com.bignerdranch or com.yourcompany. com.yourcompany。
From the Language pop-up menu, choose Swift, and from the User Interface, choose Storyboard. Make 从 语言 弹出菜单中选择 Swift,从 用户界面 中选择 故事板。确保 使用核心数据 复选框未选中。
sure that the Use Core Data checkbox is not checked.
Figure 1.3 Configuring a new project 图1.3 配置新建项目
Click Next and, in the final sheet, save the project in the directory where you plan to store the exercises 点击 下一步,在最后一个面板中,将项目保存在您计划存储本书练习的目录中。点击 创建 创建 Quiz 项目。
in this book. Click Create to create the Quiz project.
3 3
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Your new project opens in the Xcode workspace window (Figure 1.4). 您的新项目将在 Xcode 工作区窗口中打开(图 1.4)。
Figure 1.4 Xcode workspace window 图1.4 Xcode工作区窗口
The lefthand side of the workspace window is the navigator area. This area displays different 工作区窗口的左侧是导航区域。该区域显示不同的导航器——显示项目不同部分的工具。您可
navigators – tools that show you different parts of your project. You can open a navigator by selecting 以通过选择导航器选择器中的图标来打开导航器,该选择器是导航区域顶部的条形。
one of the icons in the navigator selector, which is the bar at the top of the navigator area.
4 4
Creating an Xcode Project 创建Xcode项目
The navigator currently open is the project navigator. The project navigator shows you the files that 当前打开的导航器是 项目导航器。项目导航器显示构成项目的文件(图1.5)。您可以选择其
make up a project (Figure 1.5). You can select one of these files to open and work with it in the editor 中一个文件,在导航区域右侧的 编辑区域 中打开并处理它。
area to the right of the navigator area.
The files in the project navigator can be grouped into folders to help you organize your project. A few 项目导航器中的文件可以分组到文件夹中,以帮助您组织项目。模板已为您创建了一些组。如果您想的话,可
groups have been created by the template for you. You can rename them, if you want, or add new ones. 以重命名它们,或者添加新的。
Figure 1.5 Quiz application’s files in the project navigator 图1.5 问答应用程序在项目导航器中的文件
The righthand side of the workspace window is the inspector area, which you will learn about later in 工作区窗口的右侧是 检查器区域,您将在本章后面学习它。
this chapter. 在本章中。
5 5
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Model-View-Controller 模型‑视图‑控制器
Before you begin your application, let’s discuss a key concept in application architecture: 在您开始应用程序之前,让我们讨论应用程序架构中的一个关键概念:模型-视图-控制器,
Model-View-Controller, or MVC. MVC is a design pattern used in iOS development. In MVC, every 或 MVC。MVC 是 iOS 开发中使用的 一种设计模式。在 MVC 中,每个实例都属于 模型层、
instance belongs to either the model layer, the view layer, or the controller layer. (Layer here simply
视图层 或 控制器层。(层 在这里仅指共同完成一个角色的一个或多个对象。)
refers to one or more objects that together fulfill a role.)
• The model layer holds data and knows nothing about the user interface, or UI. In Quiz, the model • 模型层<code>model layer</code>包含数据,对用户界面(UI)一无所知。在<code>Quiz</code>中,
will consist of two ordered lists of strings: one for questions and another for answers. 模型将包含两个有序的字符串列表:一个用于问题,另一个用于答案。
Usually, instances in the model layer represent real things in the world of the user. For example, 通常,模型层中的实例代表用户世界中的真实事物。例如,当你为一家保险公司编写应用程
when you write an app for an insurance company, your model will almost certainly contain a 序时,你的模型几乎肯定会包含一个自定义类型,称为<code>InsurancePolicy</code>。
custom type called InsurancePolicy.
• The view layer contains objects that are visible to the user. Examples of view objects, or views, • 视图层<code>view layer</code>包含对用户可见的对象。<code>view objects</code>或
are buttons, text fields, and sliders. View objects make up an application’s UI. In Quiz, the labels <code>views</code>的示例是按钮、文本字段和滑块。视图对象构成应用程序的UI。在
showing the question and answer and the buttons beneath them are view objects. <code>Quiz</code>中,显示问题和答案的标签以及它们下方的按钮是视图对象。
• The controller layer is where the application is managed. Controller objects, or controllers, are • 控制器层<code>controller layer</code>是应用程序管理的地方。<code>controller
the managers of an application. Controllers configure the views that the user sees and make sure objects</code>或<code>controllers</code>是应用程序的管理者。控制器配置用户看到的视图,并确
that the view and model objects stay synchronized. 保视图和模型对象保持同步。
In general, controllers typically handle “And then?” questions. For example, when the user selects 通常情况下,控制器通常处理“然后呢?”类型的问题。例如,当用户从列表中选择一个项目时,控制器
an item from a list, the controller determines what the user sees next. 会确定用户接下来看到什么。
Figure 1.6 shows the flow of control in an application in response to user input, such as the user 图1.6显示了应用程序在响应用户输入(例如用户点击按钮)时的控制流程。
tapping a button.
Figure 1.6 MVC pattern 图1.6 MVC模式
Notice that models and views do not talk to each other directly; controllers sit squarely in the middle of 请注意,模型和视图不直接相互通信;控制器位于所有内容的正中间,接收消息并分发指令。
everything, receiving messages and dispatching instructions.
6 6