Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Implementing action methods 实现动作方法
Now that you have questions and answers, you can finish implementing the action methods. In 既然您已经有了问题和答案,就可以完成动作方法的实现。在<code>ViewController.swift</code>中,
ViewController.swift, update showNextQuestion(_:) and showAnswer(_:). 更新<code>showNextQuestion(_:)</code>和<code>showAnswer(_:)</code>。
... ...
@IBAction func showNextQuestion(_ sender: UIButton) { @IBAction func 显示下一步问题(_ sender: UIButton) {
currentQuestionIndex += 1 currentQuestionIndex += 1
if currentQuestionIndex == questions.count { 如果当前问题索引 == 问题数量
currentQuestionIndex = 0 currentQuestionIndex = 0
} }
let question: String = questions[currentQuestionIndex] let question: 字符串 = 问题[当前问题索引]
questionLabel.text = question questionLabel.text = 问题
answerLabel.text = "???" answerLabel.text = "???"
} }
@IBAction func showAnswer(_ sender: UIButton) { @IBAction func showAnswer(_ sender: UIButton) {
let answer: String = answers[currentQuestionIndex] let answer: 字符串 = 答案[当前问题索引]
answerLabel.text = answer answerLabel.text = 答案
} }
... ...
Loading the first question 加载第一个问题
Just after the application is launched, you will want to load the first question from the array and use 应用程序启动后,您希望从数组中加载第一个问题,并使用它来替换 ??? 占位符在 问题标签
it to replace the ??? placeholder in the questionLabel label. A good way to do this is by overriding 标签中。一个好的方法是 重写 viewDidLoad() 方法 视图控制器。(“重写”意味着您正在为
the viewDidLoad() method of ViewController. (“Override” means that you are providing a custom
方法提供自定义实现。)将方法添加到 ViewController.swift。
implementation for a method.) Add the method to ViewController.swift.
class ViewController: UIViewController { class ViewController: UIViewController {...override func
... viewDidLoad() { super.viewDidLoad() questionLabel.text = questions[
override func viewDidLoad() {
currentQuestionIndex]}}
super.viewDidLoad()
questionLabel.text = questions[currentQuestionIndex]
}
}
All the code for your application is now complete! 您应用程序的所有代码现已完成!
26 26
Building the Finished Application 构建完成的应用
Building the Finished Application 构建完成的应用
Build and run the application on the iPhone 11 Pro simulator, as you did earlier. You should see the 在 iPhone 11 Pro 模拟器上构建并运行应用程序,就像您之前所做的那样。您应该看到第一个问题显示在
first question displayed in the top label. 顶部标签中。
If building turns up any errors, you can view them in the issue navigator by selecting the tab in the 如果构建过程中出现任何错误,您可以通过在导航区域选择标签,在 问题导航器 中查看这些错误。图 1.27
navigator area. Figure 1.27 shows the issue navigator with errors we added as examples. 显示了我们添加作为示例的错误的问题导航器。
Figure 1.27 Issue navigator with example errors 图 1.27 带有示例错误的问题导航器
Click on any error or warning in the issue navigator to be taken to the file and the line of code where 点击问题导航器中的任何错误或警告,将跳转到发生问题的文件和代码行。通过将您的代码
the issue occurred. Find and fix any problems (like code typos) by comparing your code with the 与本章中的代码进行比较,查找并修复任何问题(例如代码错误)。然后尝试再次运行应用
code in this chapter. Then try running the application again. Repeat this process until your application
程序。重复此过程,直到您的应用程序可以编译。
compiles.
After your application has compiled, it will launch in the iOS simulator. Play around with the Quiz 您的应用程序编译后,它将在 iOS 模拟器中启动。在 Quiz 应用程序中尝试操作。您应该能够
application. You should be able to tap the Next Question button and see a new question in the top label; 点击 下一个问题 按钮并在顶部标签中看到新问题;点击 显示答案 应该显示正确答案。如果您的
tapping Show Answer should show the right answer. If your application is not working as expected,
应用程序无法按预期工作,请检查 Main.storyboard 中的连接。
double-check your connections in Main.storyboard.
You have built a working iOS app! Take a moment to bask in the glory. 你已经构建了一个可运行的iOS应用程序!花点时间享受这份荣耀。
OK, enough basking. Your app works, but it needs some spit and polish. 好的,晒够了。你的应用程序可以运行,但它需要一些润色。
27 27
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Application Icons 应用图标
While running Quiz, select Hardware → Home from the simulator’s menu. You will see that Quiz’s 在运行 Quiz 时,从模拟器的菜单中选择 硬件 → 主屏幕。你会发现 Quiz 的图标是一个无聊的默认磁贴。让
icon is a boring, default tile. Let’s give Quiz a better icon. 我们给 Quiz 一个更好的图标。
An application icon is a simple image that represents the application on the iOS Home screen. 一个应用程序图标是一个简单的图像,它代表iOS主屏幕上的应用程序。不同的设备需要不同大小的
Different devices require different-sized icons, some of which are shown in Table 1.1. 图标,其中一些显示在表1.1中。
Table 1.1 Application icon sizes by device 表1.1 按设备划分的应用程序图标尺寸
Device Application icon sizes 设备 应用程序图标尺寸
iPhone 180x180 pixels (@3x) iPhone 180x180 像素 (@3x)
120x120 像素 (@2x)
120x120 pixels (@2x)
iPad and iPad mini 152x152 pixels (@2x) iPad 和 iPad mini 152x152 像素 (@2x)
iPad Pro 167x167 pixels (@2x) iPad Pro 167x167 像素 (@2x)
(2x and 3x refer to the point sizes in Retina displays. You will read more about display resolutions in (2x 和 3x 指的是视网膜显示器中的点大小。你将在第三章中读到更多关于显示分辨率的内容。)
Chapter 3.)
We have prepared an icon image file (size 120x120) for the Quiz application. You can download 我们为 Quiz 应用准备了一个图标图像文件(大小 120x120)。你可以从
this icon (along with resources for other chapters) from www.bignerdranch.com/solutions/ www.bignerdranch.com/solutions/iOSProgramming7ed.zip 下载这个图标(以及其他章节的资
iOSProgramming7ed.zip. Unzip the resource set and find the Quiz-120.png file in the 0-Resources/ 源)。解压缩资源集合,并在 0‑Resources/ 项目应用图标 目录中找到 Quiz-120.png 文件。
Project App Icons directory.
You are going to add this icon to your application bundle as a resource. In general, there are two kinds 您将把这个图标作为 资源 添加到您的应用程序包中。通常,应用程序中有两种文件:代码和
of files in an application: code and resources. Code (like ViewController.swift) is used to create the 资源。代码(如 ViewController.swift)用于创建应用程序本身。资源包括图像、声音和 接
application itself. Resources are things like images, sounds, and Interface Builder files that are used by
口构建器 文件,这些文件由应用程序在运行时使用。
the application at runtime.
28 28
Application Icons 应用图标
In the project navigator, find Assets.xcassets. Select this file to open it, then select AppIcon from the 在项目导航器中,找到 Assets.xcassets。选择此文件打开,然后从左侧资源列表(图1.28)中选择 AppIcon。
resource list on the lefthand side (Figure 1.28).
Figure 1.28 Showing the Asset Catalog 图1.28 显示资源目录
This panel is the Asset Catalog, where you can manage all the images that your application will need. 此面板是 资源目录,您可以在此管理应用程序所需的所有图像。
29 29
Chapter 1 A Simple iOS Application 第一章 一个简单的iOS应用程序
Drag the Quiz-120.png file from Finder onto the 2x slot of the iPhone App section (Figure 1.29). This 将 Quiz-120.png 文件从 Finder 拖放到 2x 的 iPhone App 部分(图1.29)。这将把文件复制到
will copy the file into your project’s directory on the filesystem and add a reference to that file in the 文件系统上你项目目录中,并在资源目录中添加对该文件的引用。(你可以在资源目录中的
Asset Catalog. (You can Control-click on a file in the Asset Catalog and select the option to Show in
文件上控制点击并选择 在Finder中显示 选项来确认这一点。)
Finder to confirm this.)
Figure 1.29 Adding the app icon to the Asset Catalog 图1.29 将应用图标添加到资源目录
Build and run the application again. Switch to the simulator’s Home screen either by selecting 再次构建并运行应用程序。通过选择 硬件 → 主屏幕(如之前所做的那样)或使用键盘快
Hardware → Home, as you did before, or by using the keyboard shortcut Command-Shift-H. You 捷键 Command‑Shift‑H 切换到模拟器的主屏幕。你应该能看到新的图标。
should see the new icon.
(If you do not see the icon, delete the application and then build and run again to redeploy it. To (如果您看不到图标,请删除应用程序,然后重新构建并运行以重新部署它。为此,最简单的
do this, the easiest option is to reset the simulator by clicking Hardware → Erase All Content and 选项是点击 硬件 → 清除所有内容和设置... 来重置模拟器。这将删除所有应用程序并将模拟器
Settings.... This will remove all applications and reset the simulator to its default settings. You should
重置为其默认设置。下次运行应用程序时,您应该会看到应用程序图标。)
see the app icon the next time you run the application.)
30 30