【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. NSWindow
declares 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 ...
随机推荐
- XStream 快速转换xml
项目地址:http://xstream.codehaus.org/tutorial.html (以下来源于官网) 1.Create classes to be serialized(初始化类) pub ...
- linux全部命令
linux全部命令 一.安装和登陆命令1.进入图形界面startx 2.进入图形界面init 5 3.进入字符界面init 3 4.登陆login 5.关机poweroff-p 关闭机器的时候关闭电源 ...
- cf 621D
http://acm.zzkun.com/archives/717 这个大神的解答非常,额 猥琐.但是实在是太强了.感觉所有的大数都可以用 long double 了.
- 4.PopupWindow
想要弹出内容就可以考虑使用悬浮窗 布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- Android 设计中的.9.png
在 Android 的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以任意调整大小的一种图片格式“.9.png”.这种图片是用于Android开发的一种特殊的图片格式, ...
- C# 中distinct的使用
假设我们有一个类:Product public class Product { public string Id { get; set; } public string Name { get; set ...
- Xamarin Anroid开发教程之Anroid开发工具及应用介绍
Xamarin Anroid开发教程之Anroid开发工具及应用介绍 Xamarin开发Anroid应用介绍 如今智能手机已经盛行了好几年,而针对这些智能手机的软件开发也变得异常火热.但是在Andro ...
- python 根据对象和方法名,返回提供这个方法的定义的类
def find_defining_class(obj, method_name): for ty in type(obj).mro(): if method_name in ty.__dict__: ...
- POJ 1177 Picture(求周长并)
题目链接 看的HH的题解..周长有两部分组成,横着和竖着的,横着通过,sum[1] - last来计算,竖着的通过标记,记录有多少段. #include <cstdio> #include ...
- oracle系列--第三篇 Oracle的安装
在安装之前,我先说说我的电脑的配置: OS : Windows 7 32bit CPU : 3GHz Memory : 2GB Desk : 320GB ======================= ...