First iOS App_Getting Started

开始/被启动Getting Started

按照指导中的创建 iOS应用,你需要 Xcode.app 4.3及以上版本。Xcode.app 是苹果为 iOS 和 Mac OS X 整合的开发环境(或 IDE)。当你在 Mac 安装 Xcode.app 时,也得到了 iOS SDK(包含了 iOS平台 的编程接口)。To create the iOS app in this tutorial, you need Xcode 4.3 or later. Xcode is Apple’s integrated development environment (or IDE) for both iOS and Mac OS X development. When you install Xcode on your Mac, you also get the iOS SDK, which includes the programming interfaces of the iOS platform.

 

创建和测试一个新项目Create and Test a New Project

要开始开发你的应用程序,创建一个新的 Xcode 项目。

To get started developing your app, you create a new Xcode project.


bullet
To create a new project

花一些时间让自己熟悉 Xcode 为你打开的工作空间窗口。贯穿这个指导的余下部分,你将会用如下的窗口的按钮和区域识别。Take a few moments to familiarize yourself with the workspace window that Xcode opens for you. You’ll use the buttons and areas identified in the window below throughout the rest of this tutorial.

image: ../Art/workspace_window_callouts.png

如果你工作空间的实用工具区(utilities area)已经打开(正如以上窗口显示),你可以先关掉,因为现在不需要用,指导后面会用到再打开。视图按钮(View buttons)的最右边的按钮控制它的开、关。当实用工具区可见时,图标会变成如下状态:If the utilities area in your workspace window is already open (as it is in the window shown above), you can close it for now because you won’t need it until later in the tutorial. The rightmost View button controls the utilities area. When the utilities area is visible, the button looks like this:

image: ../Art/utilites_button.jpg

如果需要,可以点击此按钮关闭实用工具区。If necessary, click the rightmost View button to close the utilities area.

即使你还没有写任何代码,可以在应用模拟器(Xcode.app 已内置)建立并运行你的应用。正如它的名字(Simulator)所暗示的,模拟器允许你通过代码将实现你关于你的应用的想法,如果在一个 iOS-based 设备上运行,你的应用是什么样子,有哪些行为。Even though you haven’t yet written any code, you can build your app and run it in the Simulator app that is included in Xcode. As its name implies, Simulator allows you to get an idea of how your app would look and behave if it were running on an iOS-based device.

bullet
To run your app in Simulator

Xcode 完成项目创建后,模拟器会自动启动。因为你规定是一个 iPhone应用 产品(而不是 iPad 产品),模拟器展现一个看起来像 iPhone 的窗口。在模拟器的 iPhone 屏幕上,模拟器会打开你的应用,就像如下图片展示的:After Xcode finishes building your project, Simulator should start automatically. Because you specified an iPhone product (rather than an iPad product), Simulator displays a window that looks like an iPhone. On the simulated iPhone screen, Simulator opens your app, which should look like this:

image: ../Art/first_run.jpg

现在,你的应用不是很丰富:它只是简单的展示一个空白的白色屏幕。为了了解这个白色的屏幕是怎么出现的,你需要学习关于你的代码中的对象,还要知道它们是如何一起工作来启动此应用的。现在,退出模拟器(选中模拟器图标,右键单击 > 退出模拟器;注意不要退出 Xcode.app )。Right now, your app is not very interesting: it simply displays a blank white screen. To understand where the white screen comes from, you need to learn about the objects in your code and how they work together to start the app. For now, quit Simulator (choose iOS Simulator > Quit iOS Simulator; make sure that you don’t quit Xcode).

 

找出一个应用是如何开始的Find Out How an App Starts Up

因为你将你的项目以一个 Xcode 模板做基础,当你运行应用时,大部分基础应用环境是自动建立的,Xcode 创建一个应用对象(这个对象会有其他一些东西),建立运行循环(一个运行循环注册输入原始数据,使输入事件到你的应用的递送成为可能)。大部分工作是由 UIApplicationMain 函数完成的,这个函数是 UIKit 框架提供的,是在你的项目的 main.m原始数据文件 中自动调用的。Because you based your project on an Xcode template, much of the basic app environment is automatically set up when you run the app. For example, Xcode creates an application object which, among a few other things, establishes the run loop (a run loop registers input sources and enables the delivery of input events to your app). Most of this work is done by the UIApplicationMain function, which is supplied for you by the UIKit framework and is automatically called in your project’s main.m source file.


Note(注意): UIKit 框架提供所用你应用中需要的构建和管理应用程序的用户接口的类。这个框架只是 对象-导向的 框架中的一个框架。对象-导向的 框架是由 Cocoa Touch 提供的,是所有 iOS应用 的应用环境。The UIKit framework provides all the classes that an app needs to construct and manage its user interface. The UIKit framework is just one of many object-oriented frameworks provided by Cocoa Touch, which is the app environment for all iOS apps.


bullet
To look at the main.m source file

在 main.m 文件的 main函数 中调用 UIApplication函数,有一个自动释放池(@autoreleasepool{})

The main function in main.m calls the UIApplicationMain function within an autorelease pool:

@autoreleasepool {
   return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloWorldAppDelegate class]));
}

@autoreleasepool{}声明支持自动引用计数系统(ARC系统)。ARC 为应用提供自动的 对象-生命周期 管理,确保对象被需要和不再时保持存在。The @autoreleasepool statement supports the Automatic Reference Counting (ARC) system. ARC provides automatic object-lifetime management for your app, ensuring that objects remain in existence for as long as they're needed and no longer.

在调用 UIApplicationMain 创建一个 UIApplication类 的实例、一个它的委托(代理)的实例(在本指导中,应用代理命名为 HelloWorldAppDelegate,可以提供你单一视图模板)。应用代理的主要工作是提供窗口,你的应用内容都会绘制在这个窗口。应用代理还可以在应用展示出来前,完成一些应用配置任务。(Delegation是一种设计模式,一个对象按照另一个对象的利益做事,或者一个对象与另一个对象协调工作)。The call to UIApplicationMain creates an instance of the UIApplication class and an instance of the app delegate (in this tutorial, the app delegate is HelloWorldAppDelegate, which is provided for you by the Single View template). The main job of the app delegate is to provide the window into which your app’s content is drawn. The app delegate can also perform some app configuration tasks before the app is displayed. (Delegation is a design pattern in which one object acts on behalf of, or in coordination with, another object.)

一个 iOS应用中,一个窗口对象为应用的可见的内容提供一个容器,帮助递送事件到对应的应用对象,帮助应用对在设备中的定向变化做出响应。窗口本身也是可见的。In an iOS app, a window object provides a container for the app’s visible content, helps deliver events to app objects, and helps the app respond to changes in the device’s orientation. The window itself is invisible.

调用 UIApplicationMain函数 的时候,还会扫描该应用的 Info.plist 文件。这个文件是一个信息属性列表——一个有结构的键值对列表,包含了应用的信息比如应用的名称、应用图标等。The call to UIApplicationMain also scans the app’s Info.plist file. The Info.plist file is an information property list—that is, a structured list of key-value pairs that contains information about the app such as its name and icon.

bullet
To look at the property list file

因为在这个项目中选择使用 storyboard(电影、电视节目或商业广告等的情节串连图板),Info.plist文件 还包含了应用对象需要加载的 storyboard文件 的名字。一个 storyboard 包含了一个存档文件,其中有对象集,转换(transitions),还有定义一个应用的用户接口的相关的各种链接。Because you chose to use a storyboard in this project, the Info.plist file also contains the name of the storyboard file that the application object should load. A storyboard contains an archive of the objects, transitions, and connections that define an app’s user interface.

在一个 HelloWorld 应用中,storyboard文件 被命名为 MainStoryboard.storyboard (注意:Info.plist文件 只显示名字的前一部分,不显示后缀 “.storyboard”)。当应用开启,这个文件被加载、初始化的视图控制器是从该文件被实例的。一个视图控制器(view controller)是一个对象,用于管理一个区域的内容;初始的视图控制器(initial view controller)是应用启动并第一个加载的一个简单的视图控制器。In the HelloWorld app, the storyboard file is named MainStoryboard.storyboard (note that the Info.plist file shows only the first part of this name). When the app starts, MainStoryboard.storyboard is loaded and the initial view controller is instantiated from it. A view controller is an object that manages an area of content; the initial view controller is simply the first view controller that gets loaded when an app starts.

一个 HelloWorld 应用有且仅有一个视图控制器(本例中特定的是 HelloWorldViewController)。现在,HelloWorldViewController 管理由一个单一视图提供的一个区域的内容。视图(view)是一个对象,屏幕上的一个矩形区域,可以在里面绘制内容、处理由用户触摸操作引起的事件。视图还可以包含其他的视图,被称为其子视图(subviews)。当你为一个视图添加子视图,该视图被称为其子视图的双亲/父视图(parent view),其子视图的子视图被称为其子视图的孩子视图(child view)。父视图,父视图的子视图它的子视图(还有子视图的子视图,如果有的话)来自一个视图图层(view hierarchy)。一个视图控制器管理一个唯一的与之对应的视图图层。The HelloWorld app contains only one view controller (specifically, HelloWorldViewController). Right now, HelloWorldViewController manages an area of content that is provided by a single view. A view is an object that draws content in a rectangular area of the screen and handles events caused by the user’s touches. A view can also contain other views, which are called subviews. When you add a subview to a view, the containing view is called the parent view and its subview is called a child view. The parent view, its child views (and their child views, if any) form a view hierarchy. A view controller manages a single view hierarchy.


Note: 视图和视图控制器在 HelloWorld应用 在 MVC(模型-视图-控制器) 设计模式中 代表 2/3的角色 对应用对象。第三个角色是 模型对象。在 MVC 中,模型对象代表数据(比如在一个日期应用中将要做的条目或在绘图应用中的一个形状),视图对象知道怎么展现由模型对象代表的数据,控制器对象居中,在模型和视图间调解。在 HelloWorld应用中,模型对象是保持用户输入的名字的字符串。现在你不需要知道更多关于 MVC,但是,这是个好的想法,开始思考关于对象如何扮演好不同的角色的。The views and the view controller in the HelloWorld app represent two of the three roles for app objects that are defined by the design pattern called Model-View-Controller (MVC). The third role is the model object. In MVC, model objects represent data (such as a to-do item in a calendar app or a shape in a drawing app), view objects know how to display the data represented by model objects, and controller objects mediate between models and views. In the HelloWorld app, the model object is the string that holds the name that the user enters. You don’t need to know more about MVC right now, but it’s a good idea to begin thinking about how the objects in your app play these different roles.

在接下来的步骤中,你通过添加向 HelloWorldViewController 管理的视图添加三个子视图来创建一个视图层级;这三个子视图代表文本框(the text field)、标签(the label)和按钮(the button)。In a later step, you’ll create a view hierarchy by adding three subviews to the view that’s managed by HelloWorldViewController; these three subviews represent the text field, the label, and the button.

你可以在 storyboard 中直观的看到视图控制器和其视图的代表。You can see visual representations of the view controller and its view in the storyboard.


bullet
To look at the storyboard

当你打开默认的 storyboard 时,你的工作空间窗口应该类似如下图片:

When you open the default storyboard, your workspace window should look similar to this:

image: ../Art/storyboard_on_canvas_2x.png

一个 storyboard 包含场景(scenes)和跳转(或连线 segues)。场景代表一个视图控制器,跳转代表两个场景的转换。A storyboard contains scenes and segues. A scene represents a view controller, and a segue represents a transition between two scenes.

因为唯一视图模式提供一个视图控制器,storyboard 在应用中只包含有一个场景,并且没有跳转(或连线)。帆布(或画布canvas)中指向左边的箭头指向的场景称为初始场景指示器(initial scene indicator),标示出应用启动时第一个被加载的场景(提示:初始场景类似与初始视图控制器)。Because the Single View template provides one view controller, the storyboard in your app contains one scene and no segues. The arrow that points to the left side of the scene on the canvas is the initial scene indicator, which identifies the scene that should be loaded first when the app starts (typically, the initial scene is the same as the initial view controller).

帆布上看到的场景被命名为 Hello World 视图控制器,因为它是由 HelloWorldViewController 对象管理的。这个视图控制器的组成由:一些展现在 Xcode的轮廓视图(outline view)上的一些项目。轮廓视图(outline view)是出现在帆布和项目导航间的窗格/嵌板。现在,视图控制器的组成包括如下项目:The scene that you see on the canvas is named Hello World View Controller because it is managed by the HelloWorldViewController object. The Hello World View Controller scene consists of a few items that are displayed in the Xcode outline view (which is the pane that appears between the canvas and the project navigator). Right now, the view controller consists of the following items:

  • 第一响应者占位符(一个桔色立方体代表)A first responder placeholder object (represented by an orange cube).

    第一响应者(first responder)是动态的占位符,代表的对象要在程序运行中第一个接收多个事件。这些事件包括编辑-聚焦事件(比如点击一个文本框,可以调出键盘)、动作/手势事件(比如晃动设备)、动作信息(比如当点击按钮,信息被传递)等等。在这次的指导中,你不用做任何关于第一响应者的东西。The first responder is a dynamic placeholder that represents the object that should be the first to receive various events while the app is running. These events include editing-focus events (such as tapping a text field to bring up the keyboard), motion events (such as shaking the device), and action messages (such as the message a button sends when the user taps it), among others. You won’t be doing anything with the first responder in this tutorial.

  • 一个占位符对象命名为出口(Exit),对于循环序列A placeholder object named Exit for unwinding seques.

    默认地,当一个用户解除一个子场景,其视图控制器循环(或返回)到父场景——原始传递到子场景的父场景。不管怎样,出口对象可以使一个视图控制器循环到任意的场景。By default, when a user dismisses a child scene, the view controller for that scene unwinds (or returns) to the parent scene—that is the scene that originally transitioned to the child scene. However, the Exit object enables a view controller to unwind to an arbitrary scene.

  • 一个 HelloWorldViewController对象(由一个黄色球状嵌入暗色矩形框来代表)。The HelloWorldViewController object (represented by a pale rectangle inside a yellow sphere).

    当 storyboard 加载一个场景,会创建视图控制器类的实例来管理这个场景。When a storyboard loads a scene, it creates an instance of the view controller class that manages the scene.

  • 一个视图,排列在视图控制器的下边(要让这个视图显示在轮廓视图中,你要先打开 Hello World View 前边的公开三角形)。A view, which is listed below the view controller (to reveal this view in the outline view, you might have to open the disclosure triangle next to Hello World View Controller).

    视图的白色背景是你运行应用,在模拟器会看到的。The white background of this view is what you saw when you ran the app in Simulator.


Note: 一个应用的窗口对象不在 storyboard 中标示。An app’s window object is not represented in the storyboard.

帆布上场景下面的区域被称为场景码头(scene dock)。现在,场景码头展示的是视图控制器的名字(也就是 Hello World View)。别的时间,场景码头可以包含图像,代表第一响应者、出口占位符对象和视图控制器对象。The area below the scene on the canvas is called the scene dock. Right now, the scene dock displays the view controller’s name (that is, Hello World View Controller). At other times, the scene dock can contain the icons that represent the first responder, the Exit placeholder object, and the view controller object.

 

翻新/扼要重述Recap

在这一章,用 Xcode.app 创建一个基于单一视图模板的新项目,建立并运行模板定义的默认的应用。然后又学习了一些项目的基本点,例如 main.m 源文件、info.plist 文件、storyboard文件,学习了如何启动一个应用。还学习了 MVC(Model-View-Controller)设计模式 如何为你的应用对象定义不同的角色。In this chapter you used Xcode to create a new project based on the Single View template and you built and ran the default app that the template defines. Then you looked at some of the basic pieces of the project, such as the main.m source file, the Info.plist file, and the storyboard file, and learned how an app starts up. You also learned how the Model-View-Controller design pattern defines roles for the objects in your app.

下一章,将会学到关于视图控制器和其视图的知识。In the next chapter, you’ll learn more about the view controller and its view.

posted @ 2013-10-07 23:49  small英  阅读(246)  评论(0编辑  收藏  举报