文章是苹果开发者文档中控制器对象的翻译,最后部分OS X特有的没有翻译
控制器对象扮演了一个或者更多视图对象(view objects)和一个或者更多模型对象(model objects)之间的一个协调器(coordinator)或者媒介(intermediary)。在MVC设计模式中,一个控制器对象(或者简单地,控制器)翻译用户在试图对象上面所做的动作和意图,例如当用户点击一个按钮、在text field中输入文本;控制器也传达新的或者已经改变的数据给model objects。当model objects改变,例如用户打开了存储在文件系统上的一个文档,控制器传达新的模型数据给视图对象,以便于试图对象来显示这些数据。因此控制器是视图对象了解模型对象改变的渠道,反之亦然。控制器对象可以建立和协调一个应用程序的任务,并且能管理其他对象的生命周期。Cocoa framework提供了三种主要的控制器类型:coordinating controllers, view controllers (on iOS), and mediating controllers (on OS X)。
ps:苹果开发者网站文档图片。
Coordinating Controllers
![疑问](http://static.blog.csdn.net/xheditor/xheditor_emot/default/doubt.gif)
-
Responding to delegation messages and observing notifications响应委托的消息和观察的通知
-
Responding to action messages (which are are sent by controls such as buttons when users tap or click them)响应动作消息(动作消息在用户点击控件例如按钮的时候会发送)
-
Establishing connections between objects and performing other setup tasks, such as when the application launches建立对象之间的连接和执行其他的创建工作,例如当应用程序启动的时候
-
Managing the life cycle of “owned” objects管理所持有的对象的生命周期
- 一个控制器对象是一个NSobject的自定义子类的实例,在OS X中,如果一个Cocoa应用程序利用了文档架构,协调控制器是一个NSWindowController或者NSDocumentController对象。在iOS中,视图控制器包含了协调控制器的功能角色。
View Controllers
在UIKit中,视图控制器管理一个显示满屏幕内容的视图;它持有这个师徒的引用,并且可能创建和加载视图从一个nib文件。控制器管理这个视图的展示和过度到应用程序中的任何后续视图。(大多数情况下,下一个视图从右边滑进来)导航条(navigation bar)、标签栏(tab bar)以及与它们相关的显示行为,都被视图控制器对象管理和实现。视图控制器也可以显示模态视图(modal view),响应低内存警告,当方向改变时旋转视图。
在iOS中的视图控制器是UIViewController的子类的实例。UIKit提供了几种特定目的的UIViewController的子类,例如UITableViewController。你必须扩充框架的视图控制器类使得控制器在模型和视图之间传送数据。视图控制器也是许多种类框架对象的典型的委托和数据源对象。
Mediating Controllers (OS X)
A mediating controller facilitates the flow of data between view objects and model objects. When users change a value displayed in a view object, the mediating controller automatically communicates the new value to a model object for storage; and when a property of a model changes its value, the mediating controller ensures that the appropriate view object displays the changed value. Unlike other types of controller objects, they are highly reusable. For these and other reasons, mediating controllers are a central component of the Cocoa bindings technology. You drag a mediating controller from the Interface Builder library and then configure these objects to establish bindings between the controller and its view objects and its model objects. A mediating controller is typically an instance of a concrete subclass of the abstract NSController
class.