【IOS笔记】Delegation

时间:2023-11-14 11:54:56

Delegation

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

委托是一种简单而强大的模式。在此模式中,应用程序中的一个对象代表另一个对象,或与另一个对象协调工作。授权对象保留对另一个对象(委托对象)的引用,并适时向委托对象发送信息。该信息会告诉事件的委托对象,授权对象即将处理或刚处理了某个事件。委托对象可能会对该信息作出如下响应:更新其本身或应用程序中其他对象的外观或状态,在某些情况下,它会返回一个值来反映待处理的事件该如何处理。委托的主要价值在于,它可以让你轻松在一个中心对象中定义几个对象的行为。

委托模式不仅普遍用于既有的框架类,而且也可应用在应用程序的两个自定对象之间。常见的设计是将委托作为一种手段,允许子视图控制器将某些值(通常为用户输入的值)传达到父视图控制器。

Delegation and the Cocoa Frameworks  delegation和coco框架

The delegating object is typically a framework object, and the delegate is typically a custom controller object. In a managed memory environment, the delegating object maintains a weak reference to its delegate; in a garbage-collected environment, the receiver maintains a strong reference to its delegate. Examples of delegation abound in the Foundation, UIKit, AppKit, and other Cocoa and Cocoa Touch frameworks.

delegation object 是典型的框架对象,并且delegate是一个典型的控制器对象。在一个受管理的内存环境中,delegating object 保持了一个弱引用它的delegate;在垃圾收集环境中,接收者维护了一个强应用对其delegate。框架中delegation的例子,UIKit,AppKit,其它Cocoa,Cocoa Touch框架。

An example of a delegating object is an instance of the NSWindow class of the AppKit framework. NSWindowdeclares a protocol, among whose methods is windowShouldClose:. When a user clicks the close box in a window, the window object sends windowShouldClose: to its delegate to ask it to confirm the closure of the window. The delegate returns a Boolean value, thereby controlling the behavior of the window object.

delegating object 的一个例子是NSWindow类的一个实例。NSWindow声明一个协议,其中有个方法windowShouldClose。当一个用户点击关闭按钮,window 对象发送windowShouldClose给它的delegate以确认window关闭。delegate返回一个bool值,藉此控制window对象的行为。

【IOS笔记】Delegation

Delegation and Notifications    delegation和通知

The delegate of most Cocoa framework classes is automatically registered as an observer of notifications posted by the delegating object. The delegate need only implement a notification method declared by the framework class to receive a particular notification message. Following the example above, a window object posts anNSWindowWillCloseNotification to observers but sends a windowShouldClose: message to its delegate.

大多数Cocoa框架类的delegate将自动注册为delegating object 的通知观察者。委托只需要实现由框架类声明的通知方法来接收一个特定的通知消息。按照上面的例子中,一个窗口对象的发布一个NSWindowWillCloseNotification给观察者,但发送windowShouldClose:消息给它的delegate。

Data Source  数据源

A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data. The delegating object, typically a view object such as a table view, holds a reference to its data source and occasionally asks it for the data it should display. A data source, like a delegate, must adopt a protocol and implement at minimum the required methods of that protocol. Data sources are responsible for managing the memory of the model objects they give to the delegating view.

数据源几乎和delegate一样,区别在于与delegating object的关系。与UI的delegated控制不同,数据源被数据控制。delegating object典型地是一个视图对象,例如table view,持有了数据源的引用,并常常询问数据源它是否该显示。一个数据源,像一个deleate,必须继承一个协议并至少实现一些协议总必须的方法。数据源负责管理内存中的数据模型。