【IOS笔记】View Controller Basics

时间:2021-07-14 01:34:59

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设备的app只有有限的屏幕空间用于显示,因此在给用户展示信息时必须使用一些创造性的方式。有许多信息要展示的app必须要在开始的时候显示一部分内容,然后在和用户交互的过程中不断显示和隐藏内容。视图控制器对象提供了内容管理和协调内容的显示和隐藏。通过在界面中用不同控制器控制界面不同部分,你将界面分解为更小的可管理单元。

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.

在你能够在app中使用视图控制器之前,你需要了解一下ios中用于显示内容的主要的类,包括window和view。视图控制器的主要实现就是管理视图内容的显示,但是,管理视图不是视图控制器做的唯一的事情。多数视图控制器还需要与其它视图控制器通讯,协调。由于视图控制器管理着很多连接,以及视图之间的关系,因此理解起来较困难。因此,可以通过ib创建storyboard。storyboard使这些可视化,并简化了视图对象的初始化。

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 views

【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对象执行绘制,它附于window,并在window发出要求的时候进行绘制。

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

【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开发时非常重要的。因为多数app一次只能展示一部分内容,动画可以让用户看到转换是什么时候发生的,新内容是从哪里出现的。立刻发生的切换会把用户弄糊涂。

  • 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.视图很少理解它们在app中的角色。例如,图1-1显示了一个按钮,这是一个特殊的视图,被称为控件。控件知道如何在它们的区域对用户做出响应,但是它们不知道它们操作的是什么。当一个用户与一个控件交互,它发生消息给app中的其它对象。这种灵活性允许单个类为多个按钮提供实现,每个按钮处理不同的事情。?

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.

一个复杂的app需要许多视图,常常将它们组织为视图层次。它需要对视图的组合提供动画效果,出现或消失于屏幕来提供对一个大界面的解释能力。最终,保持视图类可重用,视图类不需要知道它们在app中的角色。因此app的逻辑--大脑--需要被放在另一个地方。你的视图控制器就是组合这些视图的大脑。

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.

每个视图控制器组织并控制一个视图。这个视图常常是视图层次中的root view。视图控制器是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.

你从来不会将view直接赋给window,相反你将view controller赋给window,然后view controller自动将view添加给window

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

【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.

视图控制器小心的只在需要时才加载视图。它也会在某些条件下释放视图。因此,在资源管理方面,视图控制器扮演了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.

视图控制器自然成为相关视图协调行动的理想地方。例如,当一个按钮被按下,它发送一个消息给视图控制器。尽管视图自己对任务的执行不关心,视图控制器则需要理解按下按钮意味着什么,并且应该如何响应。控制器会更新数据对象,动画或改变保存在view中的属性值,甚至将另外一个视图控制器的内容显示到屏幕上。

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.

通常,每个试图控制器只看到了整个app的一个数据子集。它清楚如何展示特定数据子集,无需知道其它数据。因此,一个app的数据模型,用户接口设计,你创建的视图控制器,三者是相互影响的。

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显示了食谱管理app的例子。app显示了三个相关,但是不同的视图。每个视图由不同的视图控制器管理。

Figure 1-4  Distinct views managed by separate view controllers

【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属性,视图控制器就拥有了它。如果这个view是一个subview,它可以被同一个视图控制器控制,或被另一个视图控制器控制。在你学习了容器视图控制器后,你将会学到如何使用多视图控制器来组织一个单独的视图层次。

  • 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.每个视图控制器与app中的一个数据子集交互。例如,photo控制器仅仅需要知道被展示的照片。

  • 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, theUITabBarController 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对象。

Figure 1-5  View controller classes in UIKit

【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提供或你自定义的,可以被分为两大类--内容视图控制器和容器视图控制器--这反映了视图控制器在app中扮演的角色。

Content View Controllers Display Content  内容视图控制器显示内容

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.

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

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   关于表视图控制器

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.

许多app显示表格化的数据。因此,ios提供了一个内建的UIViewController子类用于管理表格化数据。UITableViewController类管理一个表格视图,具有许多标准的表格行为,如选择管理,行编辑,表配置。这些额外的支持最小化了你在实现表格界面的时候需要编写的代码量。你也可以继承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 的子类,表视图控制器仍然具有指向root view的指针,但是它还有一个单独的指针指向表视图。

Figure 1-6  Managing tabular data

【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

容器视图控制器管理其它视图控制器的内容

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.每个容器管理一个视图层次,就是视图控制器做的一样。每个容器还可以添加视图到其视图层次。容器决定何时添加一个view,view应该如果调整大小以适应视图层次,但是其它的子视图控制器仍对其视图和子视图负责任。

  • 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.

内置的容器类都围绕着某项重要的用户接口原则。你使用这些容器管理的用户接口来组织复杂的app。

About Navigation Controllers   关于导航控制器

A navigation controller presents data that is organized hierarchically and is an instance of the UINavigationController 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是一个联系人管理app,它使用一个导航控制器来为用户展示联系人信息。每个页面顶部的导航条由导航控制器拥有。页面剩余的部分的内容是由一个内容控制器管理的,表示当前层次的数据信息。当用户与接口中的控件交互时,那些控件告诉导航控制器显示下一个视图控制器或退出当前视图控制器。

Figure 1-7  Navigating hierarchical data

【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 in View Controller Catalog for iOS.

如何配置和使用导航控制器对象,参见:

About Tab Bar Controllers  关于tab bar 控制器

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.

tab bar控制器是一个容器视图控制器,通过它你可以将app分解为不同的操作模式。一个tab bar控制器时一个UITabBarController类的实例。tab bar具有多个tab,每个代表一个子视图控制器。选择一个tab后,tab bar控制器就会在屏幕上显示对应的视图控制器。

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 app的几种显示模式。每个模式具有一个内容视图控制器来管理主内容区域。在这个例子中,时钟和闹钟视图都显示了一个导航风格界面来完成其它额外操作。其它模式都使用了一个单视图界面。

Figure 1-8  Different modes of the Clock app

【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.

使用tab bar控制器可管理app中不同类型的数据,或者用不同方式来展现相同的数据。

For information about how to configure and use a tab bar controller, see Tab Bar Controllers in View Controller Catalog for iOS.

更多详情参见:

About Split View Controllers  关于split view控制器

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 the UISplitViewController class. The contents of a split view interface are derived from two child view controllers.

split view控制器讲屏幕分为多个部分,每一部分可以单独更新。split view控制器的外观会依据方向的不同而不同。一个split view控制器是一个UISplitViewController类的实例。split view的内容来自两个继承的子视图控制器。

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显示了一个带split view控制器的例子。中纵向模式,只有detail view显示,list view通过popover方式显示。然而,在横向模式,split view控制器同时显示了这两个视图。

Figure 1-9  A master-detail interface in portrait and landscape modes

【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.

split view控制器仅仅做ipad上支持,设计它是为了让你充分利用大屏幕设备的优势。在ipad中,它们是实现主细风格界面的首选方式。

For information about how to configure and use a split view controller, see Popovers in View Controller Catalog for iOS.

详情参见:

About Popover Controllers   关于popover控制器

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 apopover. In an iPad app, you can use popover controllers (UIPopoverController) to implement popovers in your own app.

图1-9中,当split view控制器以纵向方式显示时,主视图在一个特殊控件中显示--popover。在一个ipad app中,你可以使用popover控制器(UIPopoverController)来实现popovers。

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.

一个popover控制器实际上不是一个容器。它也不是继承自UIViewController类。但是,实际上,popover控制器与一个容器有些类似,因此,中使用它们的时候,你可以使用相同的编程规则。

For information about how to configure and use a popover controller, see Popovers in View Controller Catalog for iOS.

详情参见:

About Page View Controllers   page view控制器

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 the UIPageViewController 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.

page view控制器是一个容器控制器来实现page布局效果。那个布局允许用户像翻书一样在不同页面见滑动。一个page view控制器是一个UIPageViewController的实例。每个内容页由一个内容视图控制器提供。page view控制器管理页面之间的切换。当请求新页面时,page view控制器调用一个相关的数据源来获取下一页的视图控制器。

For information about how to configure and use a page view controller, see Page View Controllers in View Controller Catalog for iOS.

详情参见:

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:

当一个视图控制器内容对用户可见,它必须与一个window关联。有多种方式来做到这点:

  • Make the view controller a window’s root view controller. 让这个视图控制器成为window的主视图控制器

  • Make the view controller a child of a container.  让这个视图控制器成为容器的“孩子”

  • Show the view controller in a popover control.通过popover控件显示视图控制器

  • 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显示了联系人app的一个例子。当用户点击加号按钮来增加新联系人,联系人视图控制器显示了新联系人视图控制器。新联系人界面会保持显示,直到用户取消此操作或者填写了充足的信息可以保存了。这时信息会被传递给联系人视图控制器,然后关闭当前页面。

Figure 1-10  Presenting a view controller

【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.

当前的视图控制器可以是内容视图控制器,也可以是容器控制器。实际上,内容视图控制器就是设计由其它控制器来展示的,因此,可以将其看作内容视图控制器的一个变体。尽管容器视图控制器在被管理的视图控制器间定义了特别的关系,using 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.

多数时候,你展示视图控制器来为了从用户处收集信息。当目标达成,被展示的视图控制器被取消,并返回到app界面。

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.

被展示的视图控制器自己再展示其它视图控制器是没有意义的。这种将视图控制器串联起来的能力对于顺序展示几个模态操作是有益的。

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.

当展现一个视图控制器,一个视图控制器决定使用多大的屏幕空间。屏幕的这一部分被称作表示上下文,缺省的,表示上下文覆盖window。

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

View Controllers Work Together to Create an App’s Interface 视图控制器协作创建app用户接口

View controllers manage their views and other associated objects, but they also work with other view controllers to provide a seamless user interface. The distribution of work and communication between your app’s view controllers is an essential part of working with them. Because these relationships are so important to building complex apps, this next section reviews the relationships already discussed and describes them in more detail.

视图控制器管理它们的视图和相关对象,但是它们与其它视图控制器协作来提供流畅的用户接口。视图控制器间任务的分配和通讯是使用视图控制器的主要工作。因为这些关系对于构建复杂的app是如此重要,下面就详细讨论这点。

Parent-Child Relationships Represent Containment   父子关系展示包含

A view controller hierarchy starts with a single parent, the root view controller of a window. If that view controller is a container, it may have children that provide content. Those controllers, in turn, may also be containers with children of their own. Figure 1-11 shows an example of a view controller hierarchy. The root view controller is a tab view controller with four tabs. The first tab uses a navigation controller with children of its own and the other three tabs are managed by content view controllers with no children.

一个视图控制器层次从单一的父节点开始--根视图控制器。如果这个视图控制器是一个容器,它可以有几个孩子来提供内容。这些子控制器又可以作为容器有自己的孩子。

Figure 1-11  Parent-child relationships

【IOS笔记】View Controller Basics

The area each view controller fills is determined by its parent. The root view controller’s area is determined by the window. In Figure 1-11, the tab view controller gets its size from the window. It reserves space for its tab bar and gives the remainder of the space to its children. If the navigation controller were the control displayed right now, it reserves space for its navigation bar and hands the rest to its content controller. At each step, the child view controller’s view is resized by the parent and placed into the parent’s view hierarchy.

每个视图控制器填充的区域由它们的父节点决定。根视图控制器的区域由window决定。以此类推,如图1-11所示,每个子视图控制器的屏幕大小由其父节点决定。

This combination of views and view controllers also establishes the responder chain for events handled by your app.

视图和视图控制器的组合也同时建立了app处理的响应链。

Sibling Relationships Represent Peers Inside a Container  兄弟关系表示容器中平等的关系

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

这种容器关系定义了它们的孩子间共享平等的关系。例如,比较tab view 控制器与导航控制器。

  • 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.

Figure 1-12  Sibling relationships in a navigation controller

【IOS笔记】View Controller Basics

Presentation Represents a Transient Display of Another Interface

Presentation 用于一个界面的短暂显示

A view controller presents another view controller when it wants that view controller to perform a task. The presenting view controller is in charge of this behavior. It configures the presented view controller, receives information from it, and eventually dismisses it. However, while it is being presented, the presented view controller’s view is temporarily added to the window’s view hierarchy.

一个视图控制器可以present另一个视图控制器来执行一些任务。

In Figure 1-13, a content view controller attached to the tab view presents a view controller to perform a task. The content controller is the presenting view controller, and the modal view controller is the presented view controller.

Figure 1-13  Modal presentation by a content view

【IOS笔记】View Controller Basics

When a view controller is presented, the portion of the screen that it covers is defined by a presentation context provided to it by another view controller. The view controller that provides the presentation context does not need be the same view controller that presented it. Figure 1-14 shows the same view controller hierarchy that is presented in Figure 1-13. You can see that the content view presented the view controller, but it did not provide the presentation context. Instead, the view controller was presented by the tab controller. Because of this, even though the presenting view controller only covers the portion of the screen provided to it by the tab view controller, the presented view controller uses the entire area owned by the tab view controller.

当一个视图控制器被presented,屏幕上覆盖的这部分被称作presentation context--由另一个视图控制器提供。提供presentation context的视图控制器不需要是提供presented的视图控制器。图1-14展示了与图1-13同一个视图层次。你可以看到content view presented了视图控制器,但是它没有提供presentation context。相反,视图控制器是被tab控制器presented的。正因此,尽管content view只覆盖了部分屏幕,但是modal却覆盖了tab 视图控制器拥有的全部屏幕。

Figure 1-14  The actual presentation is performed by the root view controller.

【IOS笔记】View Controller Basics

Control Flow Represents Overall Coordination Between Content Controllers  控制流表示内容控制器间的完整协作

In an app with multiple view controllers, view controllers are usually created and destroyed throughout the lifetime of the app. During their lifetimes, the view controllers communicate with each other to present a seamless user experience. These relationships represent the control flow of your app.

有多个视图控制器的app在运行时,始终在不断创建和销毁着视图控制器。视图控制器相互通讯以提供平滑的用户体验。这些关系代表了app的控制流。

Most commonly, this control flow happens when a new view controller is instantiated. Usually, a view controller is instantiated because of actions in another view controller. The first view controller, known as the source view controller directs the second view controller, the destination view controller. If the destination view controller presents data to the user, the source view controller usually provides that data. Similarly, if the source view controller needs information from the destination view controller, it is responsible for establishing the connection between the two view controllers.

大多数时候,当一个新的视图控制器被实例化时,会出现控制流。通常,一个视图控制器被实例化是因为另一个视图控制器中的某个操作,二者分别称作目标视图控制器和源视图控制器。如果目标视图控制器给用户展示数据,源视图控制器通常是负责提供数据的。相似的,如果源视图控制器需要来自目标视图控制器的数据,它负责建立两个视图控制器的连接。

Figure 1-15 shows the most common examples of these relationships.

图1-15展示了这些关系的最常见例子。

In the figure:在图中:

  • A child of a navigation controller pushes another child onto the navigation stack.

  • A view controller presents another view controller.

  • A view controller displays another view controller in a popover.

Figure 1-15  Communication between source and destination view controllers

【IOS笔记】View Controller Basics

Each controller is configured by the one preceding it. When multiple controllers work together, they establish a communication chain throughout the app.

每个控制器被其前一个控制器配置。当多个控制器一起工作时,它们建立了贯穿整个app的通讯链。

The control flow at each link in this chain is defined by the destination view controller. The source view controller uses the conventions provided by the destination view controller.

通讯链中的每个控制流由目标控制器定义,源视图控制器使用目标视图控制器提供的通讯约定。

  • The destination view controller provides properties used to configure its data and presentation.目标控制器提供用于配置器数据和外观的属性。

  • If the destination view controller needs to communicate with view controllers preceding it in the chain, it uses 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.如果目标控制器需要与之前的控制器通讯,则使用delegate。源控制器需要提供一个实现delegate协议的对象。

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.

为了一贯坚持这个控制模型,你需要确保目标控制器不知道源控制器太多的细节。当它需要知道通讯链中某个控制器的信息时,那就通过delegate 协议,而不是某个class。避免了控制器相互了解的太多,独立的控制器变得更加容易重用。对于阅读你代码的人,一致的控制流模型使得通讯路径更加容易理解。

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-16shows 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.

storyboard的好处

Figure 1-16  A storyboard diagram in Interface Builder

【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.

好处

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

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.

scene代表一屏幕的内容。

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:

scene之间的关系;有两种主要的关系。

  • 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.对于包含关系,使用storyboard的好处是ib可以调整子视图控制器的外观。

  • 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表示可视化的流转。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的。

    Different kinds of segues provide the common transitions needed between two different view controllers:不同类型的segue提供了常用的转换

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

    • A modal segue presents the destination view controller.

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

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

    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.

You can also create connections between a view controller and objects stored in the same scene. These outlets and actions 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.