分享

View Controller Programming Guide for iOS(二)View Controller Basics

 最初九月雪 2016-04-01

View Controller Basics

Apps running on iOS–based devices have a limited amount of screen space for displaying content and therefore must be creative in how they present information to the user. Apps that have lots of information to display must therefore only show a portion to start, and then show and hide additional content as the user interacts with the app. View controller objects provide the infrastructure for managing content and for coordinating the showing and hiding of it. By having different view controller classes control separate portions of your user interface, you break up the implementation of your user interface into smaller and more manageable units.

运行在基于iOS设备的应用程序只有有限的屏幕空间来显示内容,因此它们如何给用户呈现信息必须有创造性。那些有很多信息需要显示的应用程序在开始时因此只能显示一部分,然后当用户跟应用程序交互时显示和隐藏额外的内容。视图控制器对象为管理内容和协调内容的显示和隐藏提供了基础设置。通过拥有不同的视图控制器类控制用户界面的单独部分,你把用户界面的实现分成几个更小更容易管理的单元。

Before you can use view controllers in your app, you need a basic understanding of the major classes used to display content in an iOS app, including windows and views. A key part of any view controller’s implementation is to manage the views used to display its content. However, managing views is not the only job view controllers perform. Most view controllers also communicate and coordinate with other view controllers when transitions occur. Because of the many connections view controllers manage, both looking inward to views and associated objects and looking outward to other controllers, understanding the connections between objects can sometimes be difficult. Instead, use Interface Builder to create storyboards. Storyboards make it easier to visualize the relationships in your app and greatly simplify the effort needed to initialize objects at runtime.

在你能在应用程序中使用视图控制器之前,你需要对在iOS应用程序中用来显示内容的主要类有一个基本的理解,包括窗口和视图。任何视图控制器实现的关键部分是管理显示内容的视图。 但是,管理视图不是视图控制器执行的唯一工作。当过渡发生时,大多数视图控制器还跟其他视图控制器交流和协调。因为视图控制器管理着很多链接,包括内部的视图和相关对象的链接以及外部的其它控制器之间的链接,理解对象之间的各种链接有时候很困难。作为替代,使用界面生成器来创建故事板。故事板让应用程序中的关系的可视化更简单并大大简化初始化对象在运行时所需的努力。

Screens, Windows, and Views Create Visual Interfaces

一、屏幕,窗口,和视图创建可视化界面

Figure 1-1 shows a simple interface. On the left, you can see the objects that make up this interface and understand how they are connected to each other.

图1-1显示了一个简单的界面。左侧,你可以看见组成该界面的各种对象并理解它们是如何互相连接的。

Figure 1-1  A window with its target screen and content viewsView Controller Programming Guide for iOS(二)View Controller Basics

There are three major objects at work here:

这里有三个主要对象在工作:

  • UIScreen object that identifies a physical screen connected to the device.

    一个UIScreen 对象表示连接到设备的一个物理屏幕。

  • UIWindow object that provides drawing support for the screen.

    一个UIWindow对象为屏幕提供绘图支持。

  • A set of UIView objects to perform the drawing. These objects are attached to the window and draw their contents when the window asks them to.

    一组UIView对象来执行绘图。这些对象被连接到窗口,当窗口要求它们绘制内容时绘制。

Figure 1-2 shows how these classes (and related important classes) are defined in UIKit.

图1-2 显示了这些类(以及相关重要类)在UIKit中如何被定义。

Figure 1-2  Classes in the view system

View Controller Programming Guide for iOS(二)View Controller Basics

Although you don’t need to understand everything about views to understand view controllers, it can be helpful to consider the most salient features of views:

尽管你不需要理解关于视图的所有知识,进而理解视图控制器,但是它在考虑视图的最显著功能上很有帮助:

  • A view represents a user interface element. Each view covers a specific area. Within that area, it displays contents or responds to user events.

    一个视图表示一个用户界面元素。每个视图覆盖了一个特定的区域。在那个区域中,它显示内容或响应用户事件。

  • Views can be nested in a view hierarchy. Subviews are positioned and drawn relative to their superview. Thus, when the superview moves, its subviews move with it. This hierarchy makes it easy to assemble a group of related views by placing them in a common superview.

    在一个视图层次结构里视图可以被嵌套。子视图相对于它们的子视图定位和绘制。因此,当父视图发生移动,其子视图也跟着移动。该层次结构通过把它们放在一个通用父类中让集合一组相关视图变得容易。

  • Views can animate their property values. When a change to a property value is animated, the value gradually changes over a defined period of time until it reaches the new value. Changes to multiple properties across multiple views can be coordinated in a single animation.

    视图可以动画它们的属性值。当一个属性值的改变发生动画时,该属性值在定义的时间区内逐渐改变直到它达到目标新值。交叉在多个视图的多个属性的改变可以在一个动画里协调动画。

    Animation is critically important to iOS app development. Because most apps display only a portion of their contents at one time, an animation allows the user to see when a transition occurred and where the new content came from. An instantaneous transition might confuse the user.

     动画对于iOS应用程序开发是至关重要的。因为大多数应用程序在一个时间只显示内容的一部分,动画允许用户看到一个过渡何时发生以及新内容从哪来。一个瞬间过渡可能让用户困惑。

  • Views rarely understand the role they play in your app. For example, Figure 1-1 shows a button (titled Hello), which is a special kind of view, known as a control. Controls know how to respond to user interaction in their area, but they don’t know what they control. Instead, when a user interacts with a control, it sends messages to other objects in your app. This flexibility allows a single class (UIButton) to provide the implementation for multiple buttons, each configured to trigger a different action.

     视图很少理解它们在应用程序中扮演的角色。比如,图1-1显示了一个按钮(标题Hello), 它是视图的一种特殊形式,被称为控件。控件知道如何响应用户在该区域的交互,但是它们不知道它们该控制什么。相反,当用户跟控件发生交互时,它给应用程序中的其它对象发送消息。 该灵活性允许一个单类(UIButton)提供多个按钮的实现,每个都能触发一个不同的操作。

A complex app needs many views, often assembling them into view hierarchies. It needs to animate subsets of these views onto or off the screen to provide the illusion of a single larger interface. And finally, to keep view classes reusable, the view classes need to be ignorant of the specific role they perform in the app. So the app logic—the brains—needs to be placed somewhere else. Your view controllers are the brains that tie your app’s views together.

一个复杂的应用程序需要很多视图,这些视图都被组合进视图层次结构里。它需要动画这些视图的子集到屏幕或使其离开屏幕来提供一个更大界面的错觉。最后,保持视图类可重用,视图类需要对它们在应用程序中执行的特殊角色一无所知。所以应用程序逻辑大脑还需要被放在其它地方。视图控制器就是把应用程序的视图联系到一起的大脑。

View Controllers Manage Views

二、视图控制器管理视图

Each view controller organizes and controls a view; this view is often the root view of a view hierarchy. View controllers are controller objects in the MVC pattern, but a view controller also has specific tasks iOS expects it to perform. These tasks are defined by the UIViewController class that all view controllers inherit from. All view controllers perform view and resource management tasks; other responsibilities depend on how the view controller is used.

每个视图控制器都组织管理一个视图;该视图常常是一个视图层次结构中的根视图。视图控制器就是MVC模式里的控制器对象,但是视图控制器还有特殊任务iOS期望它去执行。这些任务由 UIViewController类定义,所有视图控制器都从它这继承。所有视图控制器执行视图和资源管理任务;其它职责取决于如何使用视图控制器。

Figure 1-3 shows the interface from Figure 1-1, but updated here to use a view controller. You never directly assign the views to the window. Instead, you assign a view controller to the window, and the view controller automatically adds its view to the window.

图 1-3显示了从图1-1中延伸而来的界面,但是这里添加了一个视图控制器。你绝不是直接把视图分派到窗口上。你应该给窗口分派一个视图控制器,然后视图控制器自动添加其视图给窗口。

Figure 1-3  A view controller attached to a window automatically adds its view as a subview of the window

图 1-3 一个附加到窗口的视图控制器自动添加其视图到窗口,使它们成为窗口的子视图

View Controller Programming Guide for iOS(二)View Controller Basics

A view controller is careful to load its view only when the view is needed. It can also release the view under certain conditions. For these reasons, view controllers play a key part in managing resources in your app.

视图控制器只在视图被需要时才小心的加载该视图。它还能在特定条件下释放视图。因为这些原因,视图控制器在管理应用程序中的资源方面起着关键作用。

A view controller is the natural place to coordinate actions of its connected views. For example, when a button is pressed, it sends a message to the view controller. Although the view itself may be ignorant of the task it performs, the view controller is expected to understand what the button press means and how it should respond. The controller might update data objects, animate or change property values stored in its views, or even bring another view controller’s contents to the screen.

视图控制器是协调其所有被管理视图的行为的原始地方。比如,当一个按钮被按下,它给视图控制器发送了一个消息。尽管视图本身可能不知道它执行的任务,但是视图控制器应该知道按钮按下意味着什么,以及它该如何响应。控制器可能更新数据对象,动画或改变存储在其视图中的属性值,或甚至把另一个视图控制器的内容显示到屏幕。

Usually, each view controller instantiated by your app sees only a subset of your app’s data. It knows how to display that particular set of data, without needing to know about other kinds of data. Thus, an app’s data model, user interface design, and the view controllers you create are all influenced by each other.

通常,应用程序实例化的每个视图控制器只显示应用程序数据的一个子集。它知道如何显示那个特殊的数据集,而不需要了解其它类型的数据。因此,一个应用程序数据模型,用户接口设计,以及你创建的视图控制器都互相影响。

Figure 1-4 shows an example of an app that manages recipes. This app displays three related but distinct views. The first view lists the recipes that the app manages. Tapping a recipe shows the second view, which describes the recipe. Tapping the recipe’s picture in the detail view shows the third view, a larger version of the photo. Each view is managed by a distinct view controller object whose job is to present the appropriate view, populate the subviews with data, and respond to user interactions within the view hierarchy.

图 1-4 显示了一个管理食谱的应用程序。该应用程序显示了三个相关但是独立的视图。 第一个视图列出了应用程序管理的食谱。点击一个食谱显示第二个视图,它描述了选中的视图。点击详细视图中的食谱图片,打开第三个视图,一张放大的图片。每个视图都有一个独立的视图控制器对象管理呈现适当的视图,用数据填充子视图,以及在视图层次结构里响应用户交互。

Figure 1-4  Distinct views managed by separate view controllersView Controller Programming Guide for iOS(二)View Controller Basics

This example demonstrates a few factors common to view controllers:

该例子说明了视图控制器的一些常见要素:

  • Every view is controlled by only one view controller. When a view is assigned to the view controller’s view property, the view controller owns it. If the view is a subview, it might be controlled by the same view controller or a different view controller. You’ll learn more about how to use multiple view controllers to organize a single view hierarchy when you learn about container view controllers.

     每个视图都只被一个视图控制器管理。当一个视图被分配给视图控制器的view属性,视图控制器就拥有了该视图。如果视图是一个子视图,它可能被同一个或不同的视图控制器控制。当你学习容器视图控制器(container view controllers)时,你将学习到更多关于如何使用多个视图控制器来组织一个单一的视图层次结构。

  • Each view controller interacts with a subset of your app’s data. For example, the Photo controller needs to know only the photo to be displayed.

    每个视图控制器跟应用程序数据的一个子集发生交互。比如,Photo controller只需要知道要显示的照片。

  • Because each view controller provides only a subset of the user experience, the view controllers must communicate with each other to make this experience seamless. They may also communicate with other controllers, such as data controllers or document objects.

     因为每个视图控制器只提供用户体验的一个子集,视图控制器必须互相交流来让该体验完美衔接。它们还可能跟其它控制器相交流,比如数据控制器或文档对象。

A Taxonomy of View Controllers

三、视图控制器的分类

Figure 1-5 shows the view controller classes available in the UIKit framework along with other classes important to view controllers. For example, the UITabBarController object manages a UITabBar object, which actually displays the tabs associated with the tab bar interface. Other frameworks define additional view controller classes not shown in this figure.

图1-5 显示了UIkit框架中可用的视图控制器类,以及其它对视图控制器很重要的类。比如,UITabBarController 对象管理一个UITabBar对象,UITabBar对象实际上显示了跟标签栏界面相关的标签。 其它框架定义的额外的视图控制器类没有显示在该图中。

Figure 1-5  View controller classes in UIKit

图1-5 UIKit中的视图控制器类

View Controller Programming Guide for iOS(二)View Controller Basics

View controllers, both those provided by iOS and those you define, can be divided into two general categories—content view controllers and container view controllers—which reflect the role the view controller plays in an app.

视图控制器,不管是那些由iOS提供的还是你自己定义的视图控制器,都可以被分为两个基本类别-内容视图控制器和容器视图控制器它们反应了视图控制器在应用程序中所扮演的角色。

Content View Controllers Display Content

1、内容视图控制器显示内容

A content view controller presents content on the screen using a view or a group of views organized into a view hierarchy. The controllers described up to this point have been content view controllers. A content view controller usually knows about the subset of the app’s data that is relevant to the role the controller plays in the app.

内容视图控制器使用一个视图或一个由一组视图组成的视图层次结构在屏幕上呈现内容。之前描述的控制器已经有内容视图控制器。一个内容视图控制器通常知道应用程序数据的子集,该数据跟控制器在应用程序中所扮演的角色有关。

Here are common examples where your app uses content view controllers:

以下列出了应用程序使用内容视图控制器的常用地方:

  • To show data to the user

    给用户显示数据 

  • To collect data from the user

     从用户那收集数据

  • To perform a specific task

     执行一个指定任务

  • To navigate between a set of available commands or options, such as on the launch screen for a game

    在一组可行命令或选项之间导航,比如一个游戏的启动屏幕 

Content view controllers are the primary coordinating objects for your app because they know the specific details of the data and tasks your app offers the user.

内容视图控制器是应用程序的主要协调对象,因为它们知道数据和应用程序提供给用户的任务的具体详情。

Each content view controller object you create is responsible for managing all the views in a single view hierarchy. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple content view controllers to manage the same view hierarchy. Similarly, you should not use a single content view controller object to manage multiple screens’ worth of content.

你创建的每个内容视图控制器对象负责管理一个单一视图层次结构中的所有视图。一个视图控制器和其视图层次结构中所有视图之间的一对一对应关系是主要的设计考虑。你不应该使用多个内容视图控制器来管理同一个视图层次。同样的,你也不应该使用一个单一内容视图控制器对象来管理多个屏幕上的内容价值。

For information about defining your content view controller and implementing the required behaviors, see “Creating Custom Content View Controllers.”

关于定义你的内容视图控制器以及实现所需的行为的信息,请看“Creating Custom Content View Controllers.”

About Table View Controllers

1)关于表格视图控制器

Many apps display tabular data. For this reason, iOS provides a built-in subclass of the UIViewController class designed specifically for managing tabular data. This class, UITableViewController, manages a table view and adds support for many standard table-related behaviors such as selection management, row editing, and table configuration. This additional support is there to minimize the amount of code you must write to create and initialize a table-based interface. You can also subclass UITableViewController to add other custom behaviors.

很多应用程序显示表格数据。因此,iOS提供了一个专门用来管理表格数据的内建的UIViewController类的子类。UITableViewController 管理一个表格视图并支持很多标准表格相关的行为,比如选择(selection)管理,行编辑,以及表格配置。这些额外的支持减少了你创建和初始化一个基于表格界面必须编写的代码总量。你还可以子类化UITableViewController来添加其它自定义行为。

Figure 1-6 shows an example using a table view controller. Because it is a subclass of the UIViewController class, the table view controller still has a pointer to the root view of the interface (through its view property) but it also has a separate pointer to the table view displayed in that interface.

图1-6 显示了一个使用表格视图控制器的例子。因为它是UIViewController 的一个子类,表格视图控制器任然有一个指向接口根视图的指针(通过其view 属性),但是它还有一个指向界面中显示的表格视图的独立指针。

Figure 1-6  Managing tabular dataView Controller Programming Guide for iOS(二)View Controller Basics

For more information about table views, see Table View Programming Guide for iOS.

Container View Controllers Arrange Content of Other View Controllers

2、容器视图控制器安排其它视图控制器的内容

A container view controller contains content owned by other view controllers. These other view controllers are explicitly assigned to the container view controller as its children. A container controller can be both a parent to other controllers and a child of another container. Ultimately, this combination of controllers establishes a view controller hierarchy.

容器视图控制器包含了其它视图控制器拥有的内容。这些其它视图控制器都明确地被作为其子视图控制器分配给容器视图控制器。一个容器控制器可以是其它控制器的父控制器也可以是另一个控制器的字控制器。最终,这些控制器的组合建立一个视图层次结构。

Each type of container view controller establishes a user interface that its children operate in. The visual presentation of this user interface and the design it imposes on its children can vary widely between different types of containers. For example, here are some ways that different container view controllers may distinguish themselves:

每种类型的容器视图控制器建立了一个让其子控制器能在里操作的用户界面。该用户界面的视觉呈现,以及它施加给子控制器的设计可以在不同类型的容器之间广阔地变化。比如,以下有一些方法让不同的容器视图控制器可以区分它们自己:

  • A container provides its own API to manage its children.

     一个容器提供它自己的API来管理其子控制器。

  • A container decides whether the children have a relationship between them and what that relationship is.

     一个容器决定子控制器之间是否有关系,以及什么关系。

  • A container manages a view hierarchy just as other view controllers do. A container can also add the views of any of its children into its view hierarchy. The container decides when such a view is added and how it should be sized to fit the container’s view hierarchy, but otherwise the child view controller remains responsible for the view and its subviews.

     一个容器跟其它视图控制器一样管理一个视图层次。一个容器还可以添加子控制器的任何视图到它的视图层次。容器决定何时添加这样的一个视图,以及它应该如何被调整尺寸以适应容器的视图层次结构,但是除此之外子视图控制器保留了对视图和其子视图的职责。

  • A container might impose specific design considerations on its children. For example, a container might limit its children to certain view controller classes, or it might expect those controllers to provide additional content needed to configure the container’s views.

     一个容器可能施加特定设计构思到其子控制器。比如,一个容器可能一些特定的视图控制器类限制其子控制器,或它可能期望那些控制器来提供额外的内容来配置容器的视图。

The built-in container classes are each organized around an important user interface principle. You use the user interfaces managed by these containers to organize complex apps.

每个内建的容器类都围绕着一个重要的用户界面原则来组织的。你可以使用由这些容器管理的用户界面来组织复杂的应用程序。

About Navigation Controllers

1)关于导航控制器

A navigation controller presents data that is organized hierarchically and is an instance of theUINavigationController class. The methods of this class provide support for managing a stack-based collection of content view controllers. This stack represents the path taken by the user through the hierarchical data, with the bottom of the stack reflecting the starting point and the top of the stack reflecting the user’s current position in the data.

导航控制器呈现按层次组织的数据,它是UINavigationController 类的一个实例。该类的方法对管理一个基于栈的内容视图控制器集合提供支持。该栈表示用户通过层次数据获取的路径,栈的末端反应起始点,栈的顶端反应用户在数据中的当前位置。

Figure 1-7 shows screens from the Contacts app, which uses a navigation controller to present contact information to the user. The navigation bar at the top of each page is owned by the navigation controller. The rest of each screen displayed to the user is managed by a content view controller that presents the information at that specific level of the data hierarchy. As the user interacts with controls in the interface, those controls tell the navigation controller to display the next view controller in the sequence or dismiss the current view controller.

图1-7 显示了通讯录应用程序的屏幕,它使用一个导航控制器来呈现联络信息给用户。每个页面顶部的导航栏是导航控制器所有。每个屏幕显示给用户的其余部分是由一个内容视图控制器管理,内容视图控制器显示数据层次中特定层的信息。当用户在界面中跟控件发生交互,那些控件告诉导航控制器显示序列中的下一个视图控制器或丢弃当前的视图控制器。

Figure 1-7  Navigating hierarchical dataView Controller Programming Guide for iOS(二)View Controller Basics

Although a navigation controller’s primary job is to manage its child view controllers, it also manages a few views. Specifically, it manages a navigation bar (that displays information about the user’s current location in the data hierarchy), a button (for navigating back to previous screens), and any custom controls the current view controller needs. You do not directly modify the views owned by the view controller. Instead, you configure the controls that the navigation controller displays by setting properties on each child view controller.

尽管一个导航控制器的主要任务是管理它的子视图控制器,但是它还管理一些视图。特别是,它管理一个导航栏(显示用户在数据层次中的当前位置信息),一个按钮(导航到前一个屏幕),以及当前视图控制器需要的任何自定义控件。你不能直接修改视图控制器拥有的视图。作为替代,你配置控件,这些控件由导航控制器显示,它们通过设置在每个子视图控制器上的属性来显示。

For information about how to configure and use navigation controller objects, see “Navigation Controllers”.

关于如何配置和使用导航控件对象的信息,请看“Navigation Controllers”.

About Tab Bar Controllers

2) 关于标签栏控制器

A tab bar controller is a container view controller that you use to divide your app into two or more distinct modes of operation. A tab bar controller is an instance of the UITabBarController class. The tab bar has multiple tabs, each represented by a child view controller. Selecting a tab causes the tab bar controller to display the associated view controller’s view on the screen.

标签栏控制器是一个容器视图控制器,你可以用它来把应用程序分成两个或更多不同的操作模式。标签栏控制器是UITabBarController 类的一个实例。标签栏有多个标签,每个表示一个子视图控制器。选择一个标签导致标签栏控制器在屏幕上显示相关视图控制器的视图。

Figure 1-8 shows several modes of the Clock app along with the relationships between the corresponding view controllers. Each mode has a content view controller to manage the main content area. In the case of the Clock app, the Clock and Alarm view controllers both display a navigation-style interface to accommodate some additional controls along the top of the screen. The other modes use content view controllers to present a single screen.

图 1-8 显示了Clock应用程序的集中模式,以及相关视图控制器之间的关系。 每个模式都有一个内容视图控制器来管理主要内容区域。 在Clock应用程序中,Clock 和 Alarm 视图控制器都显示一个导航风格的界面来沿着屏幕的顶部容纳一些额外的控件。其它模式使用内容视图控制器来呈现一个单一屏幕。

Figure 1-8  Different modes of the Clock appView Controller Programming Guide for iOS(二)View Controller Basics

You use tab bar controllers when your app either presents different types of data or presents the same data in different ways.

当你的应用程序呈现不同种类数据或者以不同方式呈现相同数据的时候,你可以使用标签栏控制器。

For information about how to configure and use a tab bar controller, see “Tab Bar Controllers”.

关于如何配置和使用一个标签栏控制器的信息,请看“Tab Bar Controllers”.

About Split View Controllers

3) 关于拆分视图控制器

A split view controller divides the screen into multiple parts, each of which can be updated separately. The appearance of a split view controller may vary depending on its orientation. A split view controller is an instance of theUISplitViewController class. The contents of a split view interface are derived from two child view controllers.

拆分视图控制器把屏幕分成多个部分,每个部分都可以被单独更新。一个拆分视图控制器的外形可能很大程度上依赖于它的方向。拆分视图控制器是UISplitViewController 类的一个实例。一个拆分视图界面的内容来源于两个子视图控制器。

Figure 1-9 shows a split view interface from the MultipleDetailViews sample app. In portrait mode, only the detail view is displayed. The list view is made available using a popover. However, when displayed in landscape mode, the split view controller displays the contents of both children side by side.

图1-9显示了MultipleDetailViews例子应用程序中的一个拆分视图界面。在竖直模式,只有详细视图被显示。列表视图用一个弹出菜单(popover)打开。然而,当屏幕处于水平模式时,拆分视图控制器一边一个显示两个子控制器的内容。

Figure 1-9  A master-detail interface in portrait and landscape modesView Controller Programming Guide for iOS(二)View Controller Basics

Split view controllers are supported on iPad only and are designed to help you take advantage of the larger screen of that device. They are the preferred way to implement master-detail interfaces in iPad apps.

拆分视图控制器仅在iPad上支持,旨在帮助您利用该设备的大屏幕。它们是iPad应用程序中实现主-细节界面的首选方法。

For information about how to configure and use a split view controller, see “Popovers”.

关于如何配置和使用一个拆分视图控制器的信息,请看“Popovers”.

About Popover Controllers

4) 关于弹出控制器

Look again at Figure 1-9. When the split view controller is displayed in portrait mode, the master views is displayed in a special control, known as a popover. In an iPad app, you can use popover controllers (UIPopoverController) to implement popovers in your own app.

再次看图1-9,当拆分视图控制器以竖直模式显示时,主视图在一个特殊的控件中显示,被称为popover。在一个iPad应用程序中,你可以使用popover控制器(UIPopoverController) 来实现弹出控件。

A popover controller is not actually a container; it does not inherent from UIViewController at all. But, in practice, a popover controller is similar to a container, so you apply the same programming principles when you use them.

For information about how to configure and use a popover controller, see “Popovers”.

弹出控制器实际上不是一个容器;它不从UIViewController继承。但是,实践中,弹出控制器跟容器相似,所以你在使用它们时可以应用相同的编程原则。

About Page View Controllers

5、关于页面视图控制器

A page view controller is a container view controller used to implement a page layout. That layout allows users to flip between discrete pages of content as if it were a book. A page view controller is an instance of theUIPageViewController class. Each content page is provided by a content view controller. The page view controller manages the transitions between pages. When new pages are required, the page view controller calls an associated data source to retrieve a view controller for the next page.

页面视图控制器是一个容器视图控制器,它用来实现一个页面布局。那个布局允许用户翻转内容的不同页,就好像翻书一样。页面视图控制器是UIPageViewController 类的一个实例。每个内容页由一个内容视图控制器提供。页面视图控制器管理页面之间的过渡。当需要新页面时,页面上hi图控制器调用一个相关的数据源来为下一个页面取回一个视图控制器。

 

For information about how to configure and use a page view controller, see “Page View Controllers”.

关于如何配置和使用一个页面视图控制器的信息,请看“Page View Controllers”.

A View Controller’s Content Can Be Displayed in Many Ways

四、一个视图控制器的内容可以以不同方式显示

For a view controller’s contents to be visible to the user, it must be associated with a window. There are many ways you can do this in your app:

要想把一个视图控制器的内容显示给用户,它必须关联一个窗口。你可以在应用程序中使用以下方法来实现:

  • Make the view controller a window’s root view controller.

     把视图控制器作为窗口的根视图控制器

  • Make the view controller a child of a container.

     把视图控制器作为一个容器的子控制器

  • Show the view controller in a popover control.

    在一个弹出控件中显示视图控制器

  • Present it from another view controller.

     从另一个视图控制器中呈现它

Figure 1-10 shows an example from the Contacts app. When the user clicks the plus button to add a new contact, the Contacts view controller presents the New Contact view controller. The New Contact screen remains visible until the user cancels the operation or provides enough information about the contact that it can be saved to the contacts database. At that point the information is transmitted to the Contacts view controller, which then dismisses the controller it presented.

图1-10 显示了Contacts应用程序的一个例子。当用户点击加号来添加一个新联系方式时,Contacts视图控制器呈现添加联系人视图控制器。添加联系人屏幕保持可见直到用户取消操作或为联系方式提供了足够的信息让其保存到联系人数据库中。在那时信息被发送给联系人视图控制器(Contacts view controller),然后丢弃呈现的控制器。

Figure 1-10  Presenting a view controllerView Controller Programming Guide for iOS(二)View Controller Basics

A presented view controller isn’t a specific type of view controller—the presented view controller can be either a content or a container view controller with an attached content view controller. In practice, the content view controller is designed specifically to be presented by another controller, so it can be useful to think of it as a variant of a content view controller. Although container view controllers define specific relationships between the managed view controllers, using presentation allows you to define the relationship between the view controller being presented and the view controller presenting it.

被呈现的视图控制器没有指定类型可以是内容视图控制器也可以是带有一个内容司徒控制器的容器视图控制器。 实践中,内容视图控制器是专门设计为能让另一个控制器呈现,因此把它想象成一个内容视图控制器的变量(variant)会很有帮助。尽管容器视图控制器定义了被管理控制器之间的特定关系,但是使用presentation允许你定义被呈现的视图控制器和呈现它的视图控制器之间的关系。

Most of the time, you present view controllers to gather information from the user or capture the user’s attention for some specific purpose. Once that purpose is completed, the presenting view controller dismisses the presented view controller and returns to the standard app interface.

大多数时间,你呈现视图控制器来收集用户西诺西或为一些特定目的捕捉用户的注意力。一旦那个目的完成,presenting视图控制器就丢弃被呈现的视图控制器并返回到标准应用程序界面。

It is worth noting that a presented view controller can itself present another view controller. This ability to chain view controllers together can be useful when you need to perform several modal actions sequentially. For example, if the user taps the Add Photo button in the New Contact screen in Figure 1-10 and wants to choose an existing image, the New Contact view controller presents an image picker interface. The user must dismiss the image picker screen and then dismiss the New Contact screen separately to return to the list of contacts.

一个被呈现的视图控制器本身还可以呈现另一个视图控制器,这点值得注意。 当你需要线性的执行多个模型动作时,这个把视图控制器链接到一起的功能会很有用。比如,如果用户点击点击图1-10中的New Contact屏幕上的添加照片按钮,想要选择一张已经存在的图片,New Contact视图控制器呈现一个图片选择器界面。用户必须丢弃图片选择器屏幕,然后单独地丢弃New Contact屏幕返回到联系人列表。

When presenting a view controller, one view controller determines how much of the screen is used to present the view controller. The portion of the screen is called the presentation context By default, the presentation context is defined to cover the window.

当呈现一个视图控制器时,一个视图控制器决定花费多少屏幕来呈现视图控制器。屏幕部分默认被称为呈现上下文(presentation context), 呈现上下文被定义用来覆盖窗口。

For more information about how to present view controllers in your app, see “Presenting View Controllers from Other View Controllers.”

关于如何在应用程序中呈现视图控制器的更多信息,请看“Presenting View Controllers from Other View Controllers.”

responder chain for events handled by your app.

该视图和视图控制器的组合还为应用程序处理事件建立了响应链。

Sibling Relationships Represent Peers Inside a Container

2、同胞关系代表一个容器内的同级

The kind of container defines the relationships (if any exists) shared by its children. For example, compare the tab view controller and navigation controller.

容器的类型定义了其子视图控制器共享的关系(如果存在)。比如,比较标签视图控制器和导航控制器。

  • In a tab view controller, the tabs represent distinct screens of content; tab bar controllers do not define a relationship between its children, although your app can choose to do so.

     在一个标签视图控制器中,标签标示内容的独立屏幕;标签栏控制器没有给它的子视图控制器定义一个关系,尽管你的应用程序可以这么做。

  • In a navigation controller, siblings display related views arranged in a stack. Siblings usually share a connection with adjacent siblings.

     在一个导航控制器中,显示同胞的相关视图被安排在一个栈中。同胞常常跟相连的同胞共享一个连接。

Figure 1-12 shows a common configuration of view controllers associated with a navigation controller. The first child, the master, shows the available content without showing all of the details. When an item is selected, it pushes a new sibling onto the navigation controller so that the user can see the additional details. Similarly, if the user needs to see more details, this sibling can push another view controller that shows the most detailed content available. When siblings have a well defined relationship as in this example, they often coordinate with each other, either directly or through the container controller. See Figure 1-15.

图1-12 显示了跟导航控制器有关的一个通用视图控制器配置。第一个子控制器,master,显示了可用的内容,不显示所有的细节。当一个数据项被选择,它推送一个新同胞到导航控制器,这样用户就能看到额外的细节。相似地,如果用户需要查看更多细节,该同胞可以推送另一个视图控制器来显示可用的最详细内容。当同胞们有像该例子中的一个良好定义,它们常常直接或通过容器控制器互相协作。参见Figure 1-15.

delegation. When the source view controller configures the destination view controller’s other properties, it is also expected to provide an object that implements the delegate’s protocol.

 如果目标视图控制器需要跟链中前一个视图控制器交流,它使用委托。当源视图控制器配置目标视图控制器的其它属性,它还应该提供一个实现委托协议的对象。

The benefit of using this control flow convention is that there is a clean division of responsibilities between each pair of source and destination view controllers. Data flows down the path when the source view controller asks the destination view controller to perform a task; the source view controller drives the process. For example, it might provide the specific data object that the destination controller should display. In the other direction, data flows up the path when a view controller needs to communicates information back to the source controller that spawned it. For example, it might communicate when the task completes.

使用该控制流约定的好处是它让没对源和目标视图控制器之间有一个清楚的责任分工。当源视图控制器要求目标视图控制器执行一个任务时,数据沿着路径往下流动;源视图控制器驱动该进程。比如,它可能提供目标控制器应该显示的特定数据。在一个方向,当一个视图控制器需要把交流信息传回催生它的源控制器时,数据沿着路径向上流动。比如它可能在任务完成时发生交流。

Also, by consistently implementing this control flow model, you ensure that destination view controllers never know too much about the source view controller that configured them. When it does know about a view controller earlier in the chain, it knows only that the class implements the delegate protocol, not the class of the class. By keeping view controllers from knowing too much about each other, individual controllers become more reusable. For someone reading your code, a consistently implemented control flow model makes it easy to see the communication path between any pair of controllers.

此外,通过持续实现这种控制流模型,你还可以确保目标视图控制器绝不会知道关于源视图控制器的太多配置信息。即使它确实了解关于链中的前一个视图控制器,它也只知道实现委托协议的类,而不是类中的类。为了防止视图控制器之间了解太多,让控制器们独立变得更加重要。对于阅读你的代码的其他人,一个持续实现的控制流模型让它能很容易就看到任何一对控制器之间的交流路径。

Storyboards Help You Design Your User Interface

六、故事板帮助你设计你的用户界面

When you implement your app using storyboards, you use Interface Builder to organize your app’s view controllers and any associated views. Figure 1-16 shows an example interface layout from Interface Builder. The visual layout of Interface Builder allows you to understand the flow through your app at a glance. You can see what view controllers are instantiated by your app and their order of instantiation. But more than that, you can configure complex collections of views and other objects in the storyboard. The resulting storyboard is stored as a file in your project. When you build your project, the storyboards in your project are processed and copied into the app bundle, where they are loaded by your app at runtime.

当你用故事板实现你的应用程序时,你可以使用界面生成器来组织你的应用程序视图控制器以及任何相关视图。图1-16显示了界面生成器上的一个界面。 Interface Builder中的可视化布局一目了然,让您可以了解您的应用程序中的流。你可以看到哪些视图控制器是由应用程序实例化的,以及它们被实例化的顺序。 更重要的是,你可以配置视图的复杂集合以及故事板中的其它对象。由此产生的故事板作为一个文件被存储在项目中。当你建立项目时,项目中的故事板文件被处理并被拷贝到应用程序束(bundle)中,在那里它们在运行时由应用程序加载。

Figure 1-16  A storyboard diagram in Interface BuilderView Controller Programming Guide for iOS(二)View Controller Basics

Often, iOS can automatically instantiate the view controllers in your storyboard at the moment they are needed. Similarly, the view hierarchy associated with each controller is automatically loaded when it needs to be displayed. Both view controllers and views are instantiated with the same attributes you configured in Interface Builder. Because most of this behavior is automated for you, it greatly simplifies the work required to use view controllers in your app.

通常,iOS可以根据需要在故事板中自动实例化视图控制器。 相似地,跟每个控制器相关的视图层次在它需要被显示时自动被加载。视图控制器和视图都以界面生成器中的相同配置被实例化。因为大多数该行为都是自动化为你完成的,所以它极大地简化了在应用程序中使用视图控制器的所需的工作。

The full details of creating storyboards are described in Xcode User Guide. For now, you need to know some of the essential terminology used when implementing storyboards in your app.

关于创建故事板的完整细节在Xcode User Guide 中有描述。目前,你需要知道一些在应用程序中实现故事板所需的必要术语。

A scene represents an onscreen content area that is managed by a view controller. You can think of a scene as a view controller and its associated view hierarchy.

一个场景表示一个屏幕上的内容区域,它由一个视图控制器管理。你可以把场景想象成一个视图控制器以及其相关的视图层次结构。

You create relationships between scenes in the same storyboard. Relationships are expressed visually in a storyboard as a connection arrow from one scene to another. Interface Builder usually infers the details of a new relationship automatically when you make a connection between two objects. Two important kinds of relationships exist:

你在同一个故事板中创建场景之间的关系。关系在一个故事板中用从一个场景到另一个场景的一个连接箭头可视化表达。当你在两个对象之间做一个连接时,界面生成器通常自动推断一个新关系的细节。存在两种重要类型的关系:

  • Containment represents a parent-child relationship between two scenes. View controllers contained in other view controllers are instantiated when their parent controller is instantiated. For example, the first connection from a navigation controller to another scene defines the first view controller pushed onto the navigation stack. This controller is automatically instantiated when the navigation controller is instantiated.

     包含表示两个场景之间的一个父子关系。当父控制器被实例化时,包含在其它视图控制器里的视图控制器也被实例化。比如,从一个导航控制器到另一个场景的第一个链接定义了把第一个视图控制器推送到导航栈。该控制器在导航控制器被实例化时自动被实例化。

    An advantage to using containment relationships in a storyboard is that Interface Builder can adjust the appearance of the child view controller to reflect the presence of its ancestors. This allows Interface Builder to display the content view controller as it appears in your final app.

     在一个故事板中使用包含关系的一个好处是Interface Builder可以调整子视图控制器的外形来反映其祖先的存在。当内容视图控制器出现在你的最终应用程序中时,这样做允许 Interface Builder 显示内容视图控制器。

  • A segue represents a visual transition from one scene to another. At runtime, segues can be triggered by various actions. When a segue is triggered, it causes a new view controller to be instantiated and transitioned onscreen.

     segue表示从一个场景到另一个场景的一个可视化过渡。在运行时,segues可以由不同的动作触发。 当一个segue被触发时,它导致一个新视图控制器被实例化并过渡到屏幕上。

    Although a segue is always from one view controller to another, sometimes a third object can be involved in the process. This object actually triggers the segue. For example, if you make a connection from a button in the source view controller’s view hierarchy to the destination view controller, when the user taps the button, the segue is triggered. When a segue is made directly from the source view controller to the destination view controller, it usually represents a segue you intend to trigger programatically.

     尽管一个segue总是从一个视图控制器到另一个,有时该过程中可以涉及第三个对象。该对象实际触发segue。 比如,如果你做一个链接从一个按钮(该按钮在源视图控制器的视图层次结构中)到目标视图控制器,当用户点击该按钮,segue被触发。当直接从源视图控制器到目标视图控制器做一个segue,它通常表示你打算通过程序触发一个segue。

    Different kinds of segues provide the common transitions needed between two different view controllers:

     不同类型的segues提供了两个不同视图控制器之间所需的常用的过渡:

    • A push segue pushes the destination view controller onto a navigation controller’s stack.

       push segue 把目标视图控制器推送到一个导航控制器的栈中。

    • A modal segue presents the destination view controller.

       modal segue 表示目标视图控制器。

    • A popover segue displays the destination view controller in a popover.

       popover segue 在一个popover里显示目标视图控制器。

    • A custom segue allows you to design your own transition to display the destination view controller.

       custom segue 允许你设计你自己的过渡来显示目标视图控制器。

    Segues share a common programming model. In this model, the destination controller is instantiated automatically by iOS and then the source view controller is called to configure it. This behavior matches the control flow model described in “Control Flow Represents Overall Coordination Between Content Controllers.”

     Segues 共享一个通用编程模型。 在该模型中,目标控制器由iOS自动实例化,然后调用源视图控制器来配置它。 该行为符合控制流模型,该模型在“Control Flow Represents Overall Coordination Between Content Controllers.” 中描述。

You can also create connections between a view controller and objects stored in the same scene. These outlets andactions enable you to carefully define the relationships between the view controller and its associated objects. Connections are not normally visible in the storyboard itself but can be viewed in the Connections Inspector of Interface Builder.

你还可以在一个视图控制器和存储在同一个场景中的对象之间创建链接。这些输出口(outlets) 和 动作(actions) 让你能仔细的定义视图控制器和其相关对象之间的关系。 关系(connections)本身在故事板中不是默认可见的,但是它们能在Interface Builder中的Connections Inspector中查看。

Next     Previous

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多