第二天-关于使用UIKit开发进行应用程序开发-2018-05-
关于使用UIKit开发进行应用程序开发
学习关于UIKit和XCode为你的iOS或tvos应用提供的基础支持
总览
UIKit框架提供了创建iOS或tvOS的应用程序所必需的核心对象。你可以使用这些对象来在屏幕上显示你的内容,来与内容进行交互,并管理与系统的交互。应用程序依赖UIKit的基本行为,并且UIKit提供了很多方法来自定义这些行为来满足你的特定需求。
(The UIKit framework provides the core objects that you need to build apps for iOS and tvOS. You use these objects to display your content onscreen, to interact with that content, and to manage interactions with the system. Apps rely on UIKit for their basic behavior, and UIKit provides many ways for you to customize that behavior to match your specific needs.)
Important
You always start the development of an iOS or tvOS app by creating a project in Xcode, Apple’s integrated development environment. If you don’t have Xcode, you can download it from the App Store. You can also download the latest version from developer.apple.com.
重要
你总是通过在Apple集成开发环境Xcode中创建工程来开始iOS或tvOS应用程序的开发。如果你没有Xcode,你可以在应用商店中下载它。当然你也可以从developer.apple.com来下载最新版本。
Xcode提供模版工程作为每一个你创建的应用程序的开始点。例如:如图一所示在Xcode中使用单独视图应用程序模版创建的一个应用程序的结构。这个模版工程提供了最小的用户界面,因此你可以立刻构建和运行你的工程和在一个设备(手机或pad或pod)或模拟器中看到结果。
(Xcode provides template projects as starting points for every app you create. For example, Figure 1 shows the structure of an app created using the single view app template in Xcode. The template projects provide a minimal user interface, so you can build and run your project immediately and see the results on a device or in the simulator.)

当你创建你的应用时,Xcode编译你的源文件并给你的工程创建一个应用程序包。应用程序包是个结构化的目录,包含了与应用程序相关的代码和资源。资源文件包括支持你的代码的图片资源,故事板文件,字符串文件和应用程序元数据。应用程序包的结构很重要,但是Xcode知道你的资源文件需要去哪,所以不要担心。
(When you build your app, Xcode compiles your source files and creates an app bundle for your project. An app bundle is a structured directory that contains the code and resources associated with the app. Resources include the image assets, storyboard files, strings files, and app metadata that support your code. The structure of the app bundle is important, but Xcode knows where your resources need to go, so don’t worry about it for now.)
必需的应用程序元数据(Required App Metadata)
系统从应用程序包中的信息属性列表(Info.plist)文件中导出关于您的应用程序配置和功能的信息。 Xcode为每个新项目模板提供了此文件的预配置版本,但您可能需要在某个时候修改此文件。 例如,如果您的应用程序依赖于特定的硬件或使用特定的系统框架,则可能需要将与这些功能相关的信息添加到此文件中。
(The system derives information about your app’s configuration and capabilities from the information property list (Info.plist) file in your app bundle. Xcode provides a preconfigured version of this file with every new project template, but you will likely need to modify this file at some point. For example, if your app relies on specific hardware, or uses specific system frameworks, you might need to add information related to those features to this file.)
你可以对Info.plist文件一项常见修改的是申明你的应用程序需要的硬件和软件。这些要求是您如何向系统传达您的应用程序需要运行的方式。例如,一个导航应用程序也许需要GPS硬件的存在才能提供转由转向的指示,如图3所示。应用市场将阻止应用程序安装在不符合应用程序要求的设备上。
(One common modification you can make to the Info.plist file is to declare your app’s hardware and software requirements. These requirements are how you communicate to the system what your app needs to run. For example, a navigation app might require the presence of GPS hardware to provide turn-by-turn directions, as shown in Figure 3. The App Store prevents an app from being installed on a device that does not meet your app’s requirements.)

关于更多你可以引入在Info.plist文件中的关键字,请参阅信息属性列表关键字参考。(For information about the keys that you can include in your Info.plist file, see Information Property List Key Reference.)
UIKit应用程序的代码结构(Code Structure of a UIKit App)
UIKit提供一些应用程序的核心对象,包括这些与系统交互的应用程序,运行应用程序的主事件循环以及在屏幕上显示你的内容。你可以按原样使用这些对象,也可以仅使用较小的修改。知道修改过哪些对象,和什么时候去修改,对于实现你的应用至关重要。(UIKit provides many of your app’s core objects, including those that interact with the system, run the app’s main event loop, and display your content onscreen. You use most of these objects as-is or with only minor modifications. Knowing which objects to modify, and when to modify them, is crucial to implementing your app.)
UIKit应用程序结构基于模型-视图-控制器(MVC)设计模式,其中对象是按照他们的用途划分的。模型对象管理应用程序的数据和业务逻辑。视图对象提供了你的数据的可视化表示。控制器对象扮演者你的模型和视图对象的桥梁,在适当的时候在他们移动数据。(The structure of UIKit apps is based on the Model-View-Controller (MVC) design pattern, wherein objects are divided by their purpose. Model objects manage the app’s data and business logic. View objects provide the visual representation of your data. Controller objects act as a bridge between your model and view objects, moving data between them at appropriate times.)
图4呈现了一个相当典型的UIKit应用程序的结构。你提供了表示应用程序的数据结构的模型对象。UIKit提供了大部分的视图对象,尽管当你需要时可以为你的数据定义自定义视图。协调你的数据对象和UIKit视图对象之间数据交换的是视图控制器和程序的委托对象。
(Figure 4 represents a fairly typical structure of a UIKit app. You provide the model objects that represent your app’s data structures. UIKit provides most of the view objects, although you can define custom views for your data, as needed. Coordinating the exchange of data between your data objects and the UIKit views are your view controllers and app delegate object.)

UIKit和Foundation框架提供了许多你可以定义应用程序模型对象的基础类型。UIKit为组织属于基于磁盘文件的数据结构提供了UIDocment对象。(谷歌翻译:UIKit提供了一个UIDocument对象,用于组织属于基于磁盘的文件中的数据结构。)Foundation框架定义了基础对象呈现字符串,数字,数组和其他数据类型。The Swift Standard Library提供了许多基础框架中可用的相同类型。
(The UIKit and Foundation frameworks provide many of the basic types that you use to define your app’s model objects. UIKit provides a UIDocument object for organizing the data structures that belong in a disk-based file. The Foundation framework defines basic objects representing strings, numbers, arrays, and other data types. The Swift Standard Library provides many of the same types available in the Foundation framework.)
UIKit在你的应用中的控制器和视图层中提供了大多数的对象。特别的是,UIKit定义了UIView类,它通常负责在屏幕上显示你的内容。(你也可以使用Metal和其他系统框架直接在屏幕上呈现内容。)UIApplication对象运行你的应用程序的主事件循环和管理你的应用程序整个生命周期。
(UIKit provides most of the objects in the controller and view layers of your app. Specifically, UIKit defines the UIView class, which is usually responsible for displaying your content onscreen. (You can also render content directly to the screen using Metal and other system frameworks.) The UIApplication object runs your app’s main event loop and manages your app’s overall life cycle.)