您的位置:首页 > 其它

如何理解delegate (委托)设计模式

2012-11-02 11:23 477 查看
delegate (委托)设计模式

iOS 编程语言是 Objective-C, 简称 OC。 谈起 OC的特性,人们常说,OC 不支持多继承。但 Delegate 弥补了这个缺陷。 有了Delegate, 在声明对象时,可以使其遵循多个协议。 从而解决了多继承问题。

Delegate ,又称为 委托或代理, 它是一种设计模式。 学习iOS开发,需要深入理解 Delegate的用法。 Apple 对Delegate 有明确的说明。http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html 但理解起来还是有些抽象。

Delegate的用途:

用于改变或控制其他对象 。

Delegate 的定义:

(1)Delegate 是一个对象, 其类型为 id (anonymous type: 匿名类型);

(2) Delegate 的引用通常是一个实例变量 (instance variable), 命名为 delegate;

(3)Delegate 内所用的方法是 访问模式 (Accessors pattern)

Delegate Message 的命名:

发给Delegate的消息 通常带有(should, will, did) 之一。

should:期待delegate返回一个值;

will:表示变化发生之前 要做的事情;

did : 表示变化发生之后 要做的事情。

Cocoa Touh 的很多类都不同程度地用到Delgete。 比如: NSTextField, NSTableView。 其中 NSTableView 还用到了 Data Source。

其实,Data Source 也是一种委托。 Data Source 减少了 View 与 Model 之间的耦合性。 其中 , NSAppplication 实现了几十个委托方法。

Delegate 使用的注意事项:

Delegate 是一个 ID 类型的对象, 同样存在创建和释放问题。 对于Data Source , 只有Data Source的使用者 (比如Table View)释放后, Data Souce 才能被释放。 否则, 就会出现crash。 因为在table view 获取数据时, 数据已经不见了。

Delegate 可用在多个场景下,比如对象间的数据交互, 不同视图之间的行为交互。 若仅仅是数据交互, 可实现的方法还有很多。 Delegate 尤其适用于视图之间的行为交互。

这里通过 UIActionsheet 和 UIAlertView 的使用给以说明。

UIActionsheet *actionsheet = [ [UIActionsheet alloc]

initWithTile:@”Are you sure?”

delegate: self

cancelButtonTitle: @”No Way!”

destructiveButtonTitle: @”Yes, I’m sure!”

otherButtonTitles: nil] ;

这里需特别注意 delegate:self 的使用方法。 它表明 当 actionsheet 的button 被按下时, delegate 会收到通知。更确切地说, delegate 的actionsheet:didDismisswithButtonIndex: 方法将被调用。 将self作为 delegate 参数传递给该方法,可以确保 actionsheet:didDismisswithButtonIndex: 被调用。

Cancelbutton 顾名思义,是取消按钮。 与此相对应, destructiveButton 是确定按钮。 通过delegate:self 设置,我们可以在 actionsheet:didDismisswithButtonIndex: 方法中判断 用户选择的是 取消操作还是确定操作。

如果没必要区分 哪个按钮给按下, 直接使用 UIAlertView 即可, 实例如下:

UIAlertView *alert = [ [UIAlertView alloc]

initWithTitle:@ “Something was done”

message: @”Everything is OK”

delegate: nil

cancelButtonTitle:@”Cancel”

OtherButtonTitles:nil ];

每个view 都有自己的delegate,同样, UIAlertView 也有自己的delegate, 如果我们想知道用户何时关闭了 AlertView 提示框,或想判断用户按下的是哪个按钮,就可以使用 delegate:self。 其实这里的alertView 只是向用户提供了一个按钮,根本无需判断用户按下的是哪个按钮,因此声明 delegate:nil 即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: