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. Lua和C之间的交互

    转自:http://blog.csdn.net/sumoyu/article/details/2592693 (一) Lua 调C函数 什么样类型的函数可以被Lua调用   typedef int ( ...

  2. 深入理解 KVC\KVO 实现机制 — KVC

    KVC和KVO都属于键值编程而且底层实现机制都是isa-swizzing,所以本来想放在一起讲的.但是篇幅有限所以就分成了两篇博文 KVO实现机制传送门 KVC概述 KVC是Key Value Cod ...

  3. ML 05、分类、标注与回归

    机器学习算法 原理.实现与实践 —— 分类.标注与回归 1. 分类问题 分类问题是监督学习的一个核心问题.在监督学习中,当输出变量$Y$取有限个离散值时,预测问题便成为分类问题. 监督学习从数据中学习 ...

  4. POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)

    题意: 给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀.从小到大输出所有符合要求的串的长度. 分析: 首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中 ...

  5. python 类访问控制

    访问限制 我们可以给一个实例绑定很多属性,如果有些属性不希望被外部访问到怎么办? Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问.看例子: ...

  6. SU Demos-03T-F Analysis-02Sutvband

    第一个脚本,生成震源扫描信号,并进行gabor变换 运行结果, 第二个脚本,利用时变滤波从和信号中重建单独的3个扫描信号 运行结果

  7. haohantechsoft-PDA软件,PDA管理软件,PDA管理系统,仓库PDA销售开单盘点软件

    为了更好服务于广大服装客户群体进行销售.盘点.调拨配送等.推出基于无线网络版移动PDA销售开单盘点软件系统.该系统支持无线3G.WIFI.GPRS系统,用户可以手持PDA在无线网络连接状态下进行销售. ...

  8. datetime与smalldatetime之间的区别

    1.一直以为smalldatetime和datetime的差别只是在于时间范围: smalldatetime的有效时间范围1900/1/1~2079/6/6datetime的有效时间范围1753/1/ ...

  9. 移动APP 中文输入法下的搜索优化

    最近做了一个移动端的搜索功能,带有suggest.实现上并没有什么可说的,但是在后续优化上,特别是在中文输入法的情况下的优化使我学到一些新东西,所以决定写一篇文章. 下面是我简化后的基本功能实现,监听 ...

  10. 关于adb驱动

    Android设备(如手机)连接PC时所需要的驱动程序,一般Android设备连接WinXP是无需安装驱动的. adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb ...