About View Controllers

View controllers are a vital link between an app’s data and its visual appearance. Whenever an iOS app displays a user interface, the displayed content is managed by a view controller or a group of view controllers coordinating with each other. Therefore, view controllers provide the skeletal framework on which you build your apps.

视图控制器是应用程序数据和其视觉外形之间的一个至关重要的链接。无论何时,应用程序显示一个用户界面,其显示的内容都是由一个或一组互相合作的视图控制器管理。因此,视图控制器给你建立的应用程序提供了骨架。

iOS provides many built-in view controller classes to support standard user interface pieces, such as navigation and tab bars. As part of developing an app, you also implement one or more custom controllers to display the content specific to your app.

iOS提供了很多内置的视图控制器类来支持标准用户界面块(piece),比如导航和标签栏。作为开发应用程序的一部分,你还可以实现一个或多个自定义控制器来显示应用程序的特定内容。

../Art/navigation_interface_2x.png

At a Glance

一、概述

View controllers are traditional controller objects in the Model-View-Controller (MVC) design pattern, but they also do much more. View controllers provide many behaviors common to all iOS apps. Often, these behaviors are built into the base class. For some behaviors, the base class provides part of the solution and your view controller subclass implements custom code to provide the rest. For example, when the user rotates the device, the standard implementation attempts to rotate the user interface; however, your subclass decides whether the user interface should be rotated, and, if so, how the configuration of its views should change in the new orientation. Thus, the combination of a structured base class and specific subclassing hooks make it easy for you to customize your app’s behavior while conforming to the platform design guidelines.

在模型-视图-控制器(MVC)设计模式里,视图控制器是传统的控制器对象,但是它们的作用常常还不止这些。视图控制器给所有iOS应用程序提供了很多通用行为。这些行为常常被内建在基础类之内。对于一些行为,基础类提供了部分解决方法,你的视图控制器子类实现自定义代码来提供其余的解决方法。 比如,当用户旋转设备,标准实现尝试去旋转用户界面;然而,你的子类决定用户界面是否应该被旋转,并且如果旋转,其视图在新方向上应该如何改变其配置。因此,当遵循平台设计指南时,一个结构性基础类和具体子类挂钩相结合让自定义应用程序行为变得简单。

A View Controller Manages a Set of Views

1、视图控制器管理一组视图

A view controller manages a discrete portion of your app’s user interface. Upon request, it provides a view that can be displayed or interacted with. Often, this view is the root view for a more complex hierarchy of views—buttons, switches, and other user interface elements with existing implementations in iOS. The view controller acts as the central coordinating agent for this view hierarchy, handling exchanges between the views and any relevant controller or data objects.

视图控制器管理应用程序用户界面的一个独立部分。根据请求,它提供一个可以被显示或能与之交互的视图。该视图常常是为一个更复杂的视图层次结构提供的根视图--按钮,开关,以及其它在iOS中已经有实现的用户界面元素。视图控制器就是该视图层次结构的中央协调机构,处理视图和任何相关控制器或数据对象的交换。

 

Relevant chapter: “View Controller Basics”

 

You Manage Your Content Using Content View Controllers

2、使用内容视图控制器管理你的内容

To present content that is specific to your app, you implement your own content view controllers. You create new view controller classes by subclassing either the UIViewController class or the UITableViewController class, implementing the methods necessary to present and control your content.

要想在应用程序里呈现特殊的内容,你可以实现你自己的内容视图控制器。你可以通过子类化 UIViewController 或 UITableViewController 类来创建新的视图控制器,并实现必要的方法来呈现并控制你的内容。

Container View Controllers Manage Other View Controllers

3、容器视图控制器管理其它视图控制器

Container view controllers display content owned by other view controllers. These other view controllers are explicitly associated with the container, forming a parent-child relationship. The combination of container and content view controllers creates a hierarchy of view controller objects with a single root view controller.

容器视图控制器显示其它视图控制器拥有的内容。这些其它视图控制器明确地与容器相关联,形成了一种父子关系。容器和内容视图控制器的组合创建了一个视图控制器对象的层次结构,其中只有一个单一的根视图控制器。

Each type of container defines its own interface to manage its children. The container’s methods sometimes define specific navigational relationships between the children. A container can also set specific restrictions on the types of view controllers that can be its children. It may also expect the view controllers that are its children to provide additional content used to configure the container.

容器的每种类型定义了它自己的界面来管理它的子视图控制器。容器方法有时候在子视图控制器之间定义了具体的导航关系。容器还能对其子视图控制器的类型设置特定限制。它还可能期待其子视图控制器来提供额外的内容以用于配置容器。

iOS provides many built-in container view controller types you can use to organize your user interface.

iOS提供了很多内建的容器视图控制器类型,你可以使用它们来组织你的用户界面。

Relevant chapter: “View Controller Basics”

 

Presenting a View Controller Temporarily Brings Its View Onscreen

4、呈现一个视图控制器只是暂时的把它的视图呈现到屏幕上

Sometimes a view controller wants to display additional information to the user. Or perhaps it wants the user to provide additional information or perform a task. Screen space is limited on iOS devices; the device might not have enough room to display all the user interface elements at the same time. Instead, an iOS app temporarily displays another view for the user to interact with. The view is displayed only long enough for the user to finish the requested action.

有时候视图控制器想给用户显示额外的信息。或者它想要用户提供额外的信息或执行一个任务。在iOS设备中的屏幕控件很有限;设备可能没有足够的空间来同时显示所有的使用界面元素。相反,iOS应用程序暂时显示另一个视图来让用户与之交互。视图只显示到用户结束请求动作。

To simplify the effort required to implement such interfaces, iOS allows a view controller to present another view controller’s contents. When presented, the new view controller’s views are displayed on a portion of the screen—often the entire screen. Later, when the user completes the task, the presented view controller tells the view controller that presented it that the task is complete. The presenting view controller then dismisses the view controller it presented, restoring the screen to its original state.

为了简化实现这样的界面所需的努力,iOS允许视图控制器呈现另一个视图控制器的内容。当你呈现内容时,视图控制器的视图被显示在屏幕的一部分--常常是整个屏幕。 然后,当用户完成任务后,被显示的视图控制器告诉视图控制器呈现任务已经完成。呈现的视图控制器然后释放它呈现的视图控制器,把屏幕恢复到它的初始状态。

Presentation behavior must be included in a view controller’s design in order for it to be presented by another view controller.

呈现行为必须按顺序包含在一个视图控制器设计中,这样它们就可以被另一个视图控制器呈现。

Storyboards Link User Interface Elements into an App Interface

5、故事板把用户界面元素链接到一个应用程序接口

A user interface design can be very complex. Each view controller references multiple views, gesture recognizers, and other user interface objects. In return, these objects maintain references to the view controller or execute specific pieces of code in response to actions the user takes. And view controllers rarely act in isolation. The collaboration between multiple view controllers also defines other relationships in your app. In short, creating a user interface means instantiating and configuring many objects and establishing the relationships between them, which can be time consuming and error prone.

用户界面设计可以非常复杂。每个视图控制器引用多个视图,手势辨认者(gesture recognizers),以及其它用户界面对象。作为回报,这些对象保持对视图控制器的引用或执行指定代码片段以响应用户所做的动作。而且视图控制器很少单独地采取行动。多个视图控制器之间的协作还定义了应用程序中的其它关系。就是说,创建一个用户界面就是实例化和配置很多对象并建立它们之间的关系,而这将很耗时并容易发生错误。

Instead, use Interface Builder to create storyboards. A storyboard holds preconfigured instances of view controllers and their associated objects. Each object’s attributes can be configured in Interface Builder, as can relationships between them.

作为替换,使用界面生成器(Interface Builder)来创建故事板(storyboards)。故事板持有视图控制器的预配置实例以及它们的相关对象。每个对象的属性都可以在界面生成器里配置,它们之间的关系也可以。

At runtime, your app loads storyboards and uses them to drive your app’s interface. When objects are loaded from the storyboard, they are restored to the state you configured in the storyboard. UIKit also provides methods you can override to customize behaviors that cannot be configured directly in Interface Builder.

在运行时,应用程序加载故事板并使用它们来管理应用程序的界面。当对象从故事板加载时,它们都被恢复到你在故事板中配置的状态。UIKit还提供了很多你可以加载的方法,你可以用它们来自定义不能直接在界面生成器中配置的各种行为。

By using storyboards, you can easily see how the objects in your app’s user interface fit together. You also write less code to create and configure your app’s user-interface objects.

通过使用故事板,你可以很容易看到的对象是如何在你的应用程序的用户界面结合在一起。你还可以用少量代码就创建并配置应用程序的用户界面对象。

How to Use This Document

二、如何使用该文档

Start by reading “View Controller Basics,” which explains how view controllers work to create your app’s interface. Next, read “Using View Controllers in Your App” to understand how to use view controllers, both those built into iOS and those you create yourself.

从阅读“View Controller Basics,” 开始,它解释了视图控制器如何创建应用程序界面。然后,阅读 “Using View Controllers in Your App” 来理解如何使用视图控制器,包括哪些内建在iOS中的视图控制器以及你自己创建的视图控制器。 

When you are ready to implement your app’s custom controllers, read “Creating Custom Content View Controllers” for an overview of the tasks your view controller performs, and then read the remaining chapters in this document to learn how to implement those behaviors.

当你准备实现应用程序的自定义视图控制器时,阅读 “Creating Custom Content View Controllers” 来了解视图控制器指定的所有任务,然后阅读该文档中的剩余章节来学习如何实现那些行为。

Prerequisites

三、预备知识

Before reading this document, you should be familiar with the content in Start Developing iOS Apps Today and Your Second iOS App: Storyboards. The storyboard tutorial demonstrates many of the techniques described in this book, including the following Cocoa concepts:

在开始阅读本文档之前,你应该先熟悉Start Developing iOS Apps Today  和 Your Second iOS App: Storyboards 中的内容。故事板教程演示了很多在本书中描述的技术,包括以下Cocoa概念:

See Also

四、同时参考

For more information about the standard container view controllers provided by UIKit, see View Controller Catalog for iOS.

关于UIKit提供的标准容器视图控制器的更多信息,请看View Controller Catalog for iOS

For guidance on how to manipulate views in your view controller, see View Programming Guide for iOS.

关于如何在视图控制器中操作视图的指南,请看View Programming Guide for iOS.

For guidance on how to handle events in your view controller, see Event Handling Guide for iOS.

关于如何在视图控制器中处理事件的指南,请看Event Handling Guide for iOS.

For more information about the overall structure of an iOS app, see iOS App Programming Guide.

关于iOS应用程序的整个结构的更多信息,请看 iOS App Programming Guide.

For guidance on how to configure storyboards in your project, see Xcode User Guide

关于如何在项目中配置故事板的指南,请看Xcode User Guide

posted on 2013-08-21 09:18  cainiaozhang  阅读(921)  评论(0编辑  收藏  举报