【IOS笔记】Delegation
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,必须继承一个协议并至少实现一些协议总必须的方法。数据源负责管理内存中的数据模型。
Definitive Discussion
Delegates and Data Sources in Concepts in Objective-C Programming
Sample Code Projects
【IOS笔记】Delegation的更多相关文章
- iOS programming Delegation and Text Input
iOS programming Delegation and Text Input 1.1 Text Fields CGRect textFieldRect = CGRectMake(40, ...
- 荼菜的iOS笔记--UIView的几个Block动画
前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...
- 【IOS笔记】View Controller Basics
View Controller Basics 视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...
- IOS笔记 1
< ![CDATA[ 笔记 UIWindows 与UIView的关系iOS的坐标系统视图层次结构视图坐标(Frame和Bounds区别)UIView的常用属性和方法坐标系统的变换UIView内容 ...
- 【转】iOS笔记-自定义控件(OC)
原文网址:http://www.jianshu.com/p/f23862eb7b8a 导读: iOS开发中,很多时候系统提供的控件并不能很好的满足我们的需求,因此,自定义控件便成为搭建UI界面中必不可 ...
- View Controller Programming Guid for iOS 笔记
1.View Controller 基础 1.1 View Controller 分类 ViewController分为container view controller 和content view ...
- iOS笔记———数据存储
应用沙盒:应用文件系统的根目录,每个应用都有独自的沙盒相互:在xcode中可以用NSHomeDirectory()函数,打印当前应用的沙盒根路径. 应用程序包:包含了所有资源文件和执行文件; * Do ...
- Xamarin开发IOS笔记:切换输入法时输入框被遮住
在进行IOS开发的过程中,出现类似微信朋友圈的交互界面,当用户遇到感兴趣的内容可以进行评论.为了方便评论输入,当出现评论输入框的时候自动将评论输入框移动至键盘的上方,这样方便边输入边查看. 当用户隐藏 ...
- 【IOS笔记】Event Delivery: The Responder Chain
Event Delivery: The Responder Chain 事件分发--响应链 When you design your app, it’s likely that you want t ...
随机推荐
- oracle 10g 学习之PL/SQL简介和简单使用(10)
PL /SQL是一种高级数据库程序设计语言,该语言专门用于在各种环境下对ORACLE数据库进行访问.由于该语言集成于数据库服务器中,所以PL/SQL代码可以对数据进行快速高效的处理.PL/SQL是 P ...
- sublime Emmet的用法及相关语法
本节来讲一下Emmet插件的用法及相关语法. Emmet插件极大的提高了编程员的编程速度,下面我们来讲讲它的具体语法: 一.生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 docty ...
- Java Hour 28 HashSet
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 28 HashSet 为查找而生 LinkedList查找效率低下, ...
- js event 2
js event 2 浏览器兼容 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...
- yuv rgb 像素格式1
===========大小============= 一般,直接采集到的视频数据是RGB24的格式 RGB24一帧的大小size=width×heigth×3 Byte, RGB32的size=wid ...
- javascript优化--04高质量编码
库和API的设计: 在参数设计中保持好的习惯:如顺序,width,height;top,right,bottom,left;如命名: 将undefined看作没有值而不要表示非特定的值: 在允许0,空 ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- Android 滑动冲突处理
要想解决滑动冲突就必须好好理解 Android 的事件分发机制.不了解 Android 事件分发机制的请先参考资料学习一下. 一般有 2 种方法 1 外部拦截法 这个非常简单,因为事件是从父 view ...
- css3 flex流动自适应响应式布局样式类
1.再说css3 flex 一旦一个容器赋予了display:flex属性,将会有以下特点: 项目无法设置浮动. 列表的样式会被清除. 无法使用vertical-align设置垂直对齐方式. 目前互联 ...
- 【BZOJ】1458: 士兵占领(上下界网络流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1458 是不是我脑洞太小了.......直接弄上下界最小流........(就当复习了.. 二分图X和 ...