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对象的行为。

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,必须继承一个协议并至少实现一些协议总必须的方法。数据源负责管理内存中的数据模型。

【IOS笔记】Delegation的更多相关文章

  1. iOS programming Delegation and Text Input

    iOS programming Delegation and Text Input  1.1 Text Fields    CGRect textFieldRect = CGRectMake(40, ...

  2. 荼菜的iOS笔记--UIView的几个Block动画

    前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...

  3. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  4. IOS笔记 1

    < ![CDATA[ 笔记 UIWindows 与UIView的关系iOS的坐标系统视图层次结构视图坐标(Frame和Bounds区别)UIView的常用属性和方法坐标系统的变换UIView内容 ...

  5. 【转】iOS笔记-自定义控件(OC)

    原文网址:http://www.jianshu.com/p/f23862eb7b8a 导读: iOS开发中,很多时候系统提供的控件并不能很好的满足我们的需求,因此,自定义控件便成为搭建UI界面中必不可 ...

  6. View Controller Programming Guid for iOS 笔记

    1.View Controller 基础 1.1 View Controller 分类 ViewController分为container view controller 和content view ...

  7. iOS笔记———数据存储

    应用沙盒:应用文件系统的根目录,每个应用都有独自的沙盒相互:在xcode中可以用NSHomeDirectory()函数,打印当前应用的沙盒根路径. 应用程序包:包含了所有资源文件和执行文件; * Do ...

  8. Xamarin开发IOS笔记:切换输入法时输入框被遮住

    在进行IOS开发的过程中,出现类似微信朋友圈的交互界面,当用户遇到感兴趣的内容可以进行评论.为了方便评论输入,当出现评论输入框的时候自动将评论输入框移动至键盘的上方,这样方便边输入边查看. 当用户隐藏 ...

  9. 【IOS笔记】Event Delivery: The Responder Chain

    Event Delivery: The Responder Chain  事件分发--响应链 When you design your app, it’s likely that you want t ...

随机推荐

  1. WPF中的画图

    1.border(边框):      <Border BorderBrush="Blue" BorderThickness="0,1,1,1" Grid. ...

  2. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  3. ytu 2011: C语言实验——找中间数(水题)

    2011: C语言实验——找中间数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 212  Solved: 122[Submit][Status][Web ...

  4. PHP 常用正则汇总

     平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用:    }|d{})-((([-]{}))|([|]))-(([-]([-]{}))|([|]))$/   ([-]{}) ...

  5. hdu 2255 二分图最大权匹配 *

    题意:说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房 ...

  6. 电赛总结(三)——DA芯片总结

    一.AD7890 1.特性参数 (1)高速12位DA,转换速度5.9us (2)具有8个通道. (3)串行通信 2.芯片管脚图 3.管脚功能 管脚名称 功能 AGND 模拟地 SMODE 控制端,&q ...

  7. Intent的七大属性

    1.Action Action属性代表系统要执行的动作 系统提供如下常用的Action属性 *ACTION_MAIN:应用程序入口点 *ACTION_VIEW:显示指定数据 *ACTION_EDIT: ...

  8. Spark服务启动的一些总结

    1.我理解常用的Spark部署方式有三种 1).本地服务,就是所谓的local,在IDE上本地跑程序,用于调试 2).Standalone,使用自己的master/worker进行服务的调度.  脱离 ...

  9. 【转载自W3CPLUS】如何将页脚固定在页面底部

    该文章转载自:W3CPLUS 大漠的文章 http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-the-page 以下为全文 作为一个Web的 ...

  10. IE6/IE7下绝对定位position:absolute和margin的冲突问题解决

    首先我们来看一个代码: 复制代码代码如下:<div id=”layer1″ style=”margin:20px; border:1px solid #F88; width:400px; “&g ...