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.html
運行在基於iOS設備的應用程序只有有限的屏幕空間來顯示內容,所以它們如何給用戶呈現信息必須有創造性。那些有不少信息須要顯示的應用程序在開始時所以只能顯示一部分,而後當用戶跟應用程序交互時顯示和隱藏額外的內容。視圖控制器對象爲管理內容和協調內容的顯示和隱藏提供了基礎設置。經過擁有不一樣的視圖控制器類控制用戶界面的單獨部分,你把用戶界面的實現分紅幾個更小更容易管理的單元。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應用程序中用來顯示內容的主要類有一個基本的理解,包括窗口和視圖。任何視圖控制器實現的關鍵部分是管理顯示內容的視圖。 可是,管理視圖不是視圖控制器執行的惟一工做。當過渡發生時,大多數視圖控制器還跟其餘視圖控制器交流和協調。由於視圖控制器管理着不少連接,包括內部的視圖和相關對象的連接以及外部的其它控制器之間的連接,理解對象之間的各類連接有時候很困難。做爲替代,使用界面生成器來建立故事板。故事板讓應用程序中的關係的可視化更簡單並大大簡化初始化對象在運行時所需的努力。express
1、屏幕,窗口,和視圖建立可視化界面編程
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.windows
圖1-1顯示了一個簡單的界面。左側,你能夠看見組成該界面的各類對象並理解它們是如何互相鏈接的。app
A window with its target screen and content views框架
There are three major objects at work here:less
這裏有三個主要對象在工做:ide
A UIScreen
object that identifies a physical screen connected to the device.
一個UIScreen 對象表示鏈接到設備的一個物理屏幕。
A 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中如何被定義。
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.
一個複雜的應用程序須要不少視圖,這些視圖都被組合進視圖層次結構裏。它須要動畫這些視圖的子集到屏幕或使其離開屏幕來提供一個更大界面的錯覺。最後,保持視圖類可重用,視圖類須要對它們在應用程序中執行的特殊角色一無所知。因此應用程序邏輯---大腦---還須要被放在其它地方。視圖控制器就是把應用程序的視圖聯繫到一塊兒的大腦。
2、視圖控制器管理視圖
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中延伸而來的界面,可是這裏添加了一個視圖控制器。你毫不是直接把視圖分派到窗口上。你應該給窗口分派一個視圖控制器,而後視圖控制器自動添加其視圖給窗口。
A view controller attached to a window automatically adds its view as a subview of the window
圖 1-3 一個附加到窗口的視圖控制器自動添加其視圖到窗口,使它們成爲窗口的子視圖
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 顯示了一個管理食譜的應用程序。該應用程序顯示了三個相關可是獨立的視圖。 第一個視圖列出了應用程序管理的食譜。點擊一個食譜顯示第二個視圖,它描述了選中的視圖。點擊詳細視圖中的食譜圖片,打開第三個視圖,一張放大的圖片。每一個視圖都有一個獨立的視圖控制器對象管理---呈現適當的視圖,用數據填充子視圖,以及在視圖層次結構裏響應用戶交互。
Distinct views managed by separate view controllers
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.
由於每一個視圖控制器只提供用戶體驗的一個子集,視圖控制器必須互相交流來讓該體驗完美銜接。它們還可能跟其它控制器相交流,好比數據控制器或文檔對象。
3、視圖控制器的分類
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對象實際上顯示了跟標籤欄界面相關的標籤。 其它框架定義的額外的視圖控制器類沒有顯示在該圖中。
View controller classes in UIKit
圖1-5 UIKit中的視圖控制器類
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提供的仍是你本身定義的視圖控制器,均可以被分爲兩個基本類別----內容視圖控制器和容器視圖控制器---它們反應了視圖控制器在應用程序中所扮演的角色。
一、內容視圖控制器顯示內容
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.」
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 屬性),可是它還有一個指向界面中顯示的表格視圖的獨立指針。
For more information about table views, see Table View Programming Guide for iOS.
二、容器視圖控制器安排其它視圖控制器的內容
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.
每一個內建的容器類都圍繞着一個重要的用戶界面原則來組織的。你可使用由這些容器管理的用戶界面來組織複雜的應用程序。
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 顯示了通信錄應用程序的屏幕,它使用一個導航控制器來呈現聯絡信息給用戶。每一個頁面頂部的導航欄是導航控制器全部。每一個屏幕顯示給用戶的其他部分是由一個內容視圖控制器管理,內容視圖控制器顯示數據層次中特定層的信息。當用戶在界面中跟控件發生交互,那些控件告訴導航控制器顯示序列中的下一個視圖控制器或丟棄當前的視圖控制器。
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」.
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 視圖控制器都顯示一個導航風格的界面來沿着屏幕的頂部容納一些額外的控件。其它模式使用內容視圖控制器來呈現一個單一屏幕。
Different modes of the Clock app
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」.
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)打開。然而,當屏幕處於水平模式時,拆分視圖控制器一邊一個顯示兩個子控制器的內容。
A master-detail interface in portrait and landscape modes
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」.
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繼承。可是,實踐中,彈出控制器跟容器類似,因此你在使用它們時能夠應用相同的編程原則。
五、關於頁面視圖控制器
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」.
4、一個視圖控制器的內容能夠以不一樣方式顯示
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),而後丟棄呈現的控制器。
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.」
5、視圖控制器一塊兒工做建立一個應用程序的界面
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.
視圖控制器管理它們的視圖以及其它相關對象,可是它們還跟其它視圖控制器一塊兒提供一個無縫的用戶界面。您的應用程序的視圖控制器之間的工做和交流的分配是與它們一塊兒工做的一個重要組成部分。由於這些關係對於創建複雜應用程序是如此重要,下一節回顧已經討論過的關係,並介紹了它們的更多細節。
一、父子關係表示包含
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.
一個視圖控制器層次結構以一個單一父控制器開始,即窗口的根視圖控制器。若是那個視圖控制器是是一個容器,它可能有提供內容的子控制器。那些子控制器相應地也多是它們本身的子控制器的容器。圖1-11 顯示了一個視圖控制器層次結構。根視圖控制器是一個帶有4個標籤的標籤視圖控制器。第一個標籤使用一個帶有本身的子控制器的導航控制器,其它三個標籤由內容視圖控制器管理,它們沒有子控制器。
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.
每一個視圖控制器填充的區域由其父控制器決定。根視圖控制器的區域是由窗口決定的。在圖1-11中,標籤視圖控制器從窗口獲取它的尺寸。它給標籤欄留出空間,並把剩餘空間給子視圖。若是導航控制器是剛剛顯示的控件,它給導航欄留出控件,並把剩餘空間留給內容控制器。在每一個步驟,子視圖控制器的視圖由父視圖從新調整尺寸並放入父視圖的視圖層次結構中。
This combination of views and view controllers also establishes the responder chain for events handled by your app.
該視圖和視圖控制器的組合還爲應用程序處理事件創建了響應鏈。
二、同胞關係表明一個容器內的同級
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.
Sibling relationships in a navigation controller
三、呈現表示另外一個界面的瞬時顯示
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.
當一個視圖控制器想要另外一個視圖控制器執行一個任務時,它呈現另外一個視圖控制器。呈現它的視圖控制器管理該行爲。它配置被呈現的視圖控制器,從它那接收信息,並最終丟棄它。然而,當它被呈現時,被呈現的視圖控制器的視圖暫時被添加到窗口的視圖層次結構中。
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.
圖1-13, 一個鏈接到標籤視圖(Tab controller)的內容視圖(Content)呈現了一個視圖控制器(Modal)來執行一個任務。Content是呈現視圖控制器,Modal視圖控制器是被呈現的視圖控制器。
Modal presentation by a content view
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 inFigure 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.
當一個視圖控制器被呈現時,它覆蓋的一部分屏幕由另外一個視圖控制器提供的呈現上下文(presentation context)定義。提供呈現上下文的視圖控制器不須要跟呈現它的視圖控制器是同一個。圖1-14顯示了跟圖1-13一樣的視圖控制器層次。你能夠看到內容視圖(content view)呈現了視圖控制器,可是它不提供呈現上下文。 相反,視圖控制器由標籤控制器所呈現。所以,即便呈現的視圖控制器只覆蓋了標籤視圖控制器提供給它的部分屏幕,可是被呈現的視圖控制器任然使用了標籤視圖控制器所擁有的整個區域。
The actual presentation is performed by the root view controller.
圖1-14 實際呈現由根視圖控制器執行
四、控制流表示內容控制器之間的整個協做
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.
擁有多個視圖控制器的應用程序,視圖控制器的建立和銷燬一般貫穿於應用程序的整個生命期。在生命期期間,視圖控制器互相交流以呈現一個無縫的用戶體驗。這些關係表明應用程序的控制流。
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.
一個視圖控制器在一個彈出菜單(popover)裏顯示另外一個視圖控制器。
Communication between source and destination view controllers
Each controller is configured by the one preceding it. When multiple controllers work together, they establish a communication chain throughout the 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 usesdelegation. 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.
此外,經過持續實現這種控制流模型,你還能夠確保目標視圖控制器毫不會知道關於源視圖控制器的太多配置信息。即便它確實瞭解關於鏈中的前一個視圖控制器,它也只知道實現委託協議的類,而不是類中的類。爲了防止視圖控制器之間瞭解太多,讓控制器們獨立變得更加劇要。對於閱讀你的代碼的其餘人,一個持續實現的控制流模型讓它能很容易就看到任何一對控制器之間的交流路徑。
6、故事板幫助你設計你的用戶界面
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)中,在那裏它們在運行時由應用程序加載。
A storyboard diagram in Interface Builder
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中查看。