objectc delegate 代理的用途

我简单的总结了一下自己用到的委托的作用有两个,一个是传值,一个是传事件。

1.所谓传值经常用在b类要把自己的一个数据或者对象传给a类,让a类去展示或者处理。(切分紧耦合,和代码分块的时候经常用)

2.所谓传事件就是a类发生了什么事,把这件事告诉关注自己的人,也就是委托的对象,由委托的对象去考虑发生这个事件后应该做出什么反映。(这个经常见,例如在异步请求中,界面事件触发数据层改变等等)

3.利用委托赋值,这种方法感觉是为了不暴露自己的属性就可以给自己复值,而且这样更方便了类的管理,只有在你想要让别人给你赋值的时候才调用,这样的赋值更可控一些。(例如tableView中的委托(dateSource)中常见)。

最后,我想分享一下在使用委托的时候的一些心得和注意事项。

心得:delegate的命名要准确,尽量看名字就知道用法。delegate和通知有的用法有些象,但是前者是单对单的,后者是单对多的情况。

注意:在dealloc要把delegate至为nil,还有就是delegate设置属性的时候要用assign,不要用retain。

stack overflow的解释

http://stackoverflow.com/questions/2534094/what-is-a-delegate-in-objective-cs-iphone-development

A delegate allows one object to send messages to another object when an event happens. For example, if you‘re downloading data from a web site asynchronously using theNSURLConnection class. NSURLConnection has three common delegates:

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connectionDidFinishLoading:(NSURLConnection *)connection - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

One or more of these delegates will get called when NSURLConnection encounters a failure, finishes successfully, or received a response from the web site, respectively.

A delegate is a pointer to an object with a set of methods the delegate-holder knows how to call. In other words, it‘sa mechanism to enable specific callbacks from a later-created object.

good example is UIAlertView. You create a UIAlertView object to show a short message box to users, possibly giving them a choice with two buttons like "OK" and "Cancel". TheUIAlertView needs a way to call you back, but it has no information of which object to call back and what method to call.

To solve this problem, you can send your self pointer to UIAlertView as a delegate object, and in exchange you agree (by declaring theUIAlertViewDelegate in your object‘s header file) to implement some methods thatUIAlertView can call, such asalertView:clickedButtonAtIndex:.

The delegate fires the automatic events in Objects C. If you set the delegate to Object, it sends the message to another object through the delegate methods.

It‘s a way to modify the behavior of a class without requiring subclassing.

Each Objects having the delegate methods.These delegate methods fires, when the particular Objects take part in user interaction and Program flow cycle.

Simply stated: delegation is a way of allowing objects to interact with each other without creating strong interdependencies between them.

时间: 2024-07-29 17:14:59

objectc delegate 代理的用途的相关文章

iOS delegate, 代理/委托与协议.

之前知知道iOS协议怎么写, 以为真的跟特么java接口一样, 后来发现完全不是. 首先, 说说应用场景, 就是当你要用一个程序类, 或者说逻辑类, 去控制一个storyboard里面的label, 发现如果直接用 UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];//由storyboard根据myView的storyBoardID来

@protocol(协议)和@delegate(代理)的个人理解 通俗易懂

protocol:中文叫协议,一个只有方法体(没有具体实现)的类,Java中称作接口,实现协议的类必须实现协议中@required标记的方法(如果有的话): delegate:中文叫代理或委托,是一种设计模式.以协议的方式去体现(可以理解为协议的一种),区别在于代码中常以<xxxDeletgate>标示,以便清晰辨明此为代理. 下面以一个ViewController要绘制TableView来进行说明: 1.要绘制TableView,首先要实现两个协议 UITableViewDataSource

第七讲.NSDate,category(分类),extension(延展),protocol(协议),delegate(代理)

一.掌握NSDate和NSDateFormatter的使用 NSDate是Cocoa中?用于处理日期和时间的基础类,封装了某一给定的时刻(含日期,时间,时区). 了解NSDate的创建,如何计算当前时间,时间间隔,以及时间日期与字符之间的互转. 1>.NSDate创建,当地时间计算,时间按间隔计算,时间与字符之间的互转 练习题1:计算当前时间与一个固定时间按的时间差 练习二:字符串转化为日期对象 二.掌握Category的定义和使用(只能为类扩展类方法或实例方法) 1.主要作用是为:没有源代码的

ios delegate 代理模式 观察者模式 不同视图间的通信

delegate,在ios中比比皆是,NSURLConnection(网络请求有),tableView, connectionView,等系统自带 的常见代理.甚至,自己写代码的时候,随意间敲打出了protocol,根据需求,就出来了个delegate.其实也就是观察者模式的具体应用.基本的observer.一样的思想,就是创建一个抽象方法,到具体的视图控制器中才去实现.最常见的做法是:创建单例模式,然后,调用对象,设置代理对象,写具体的实现. NSNotification通知信号虽然也能达到同

关于delegate(代理)总结

stackoverflow  上讲解:http://stackoverflow.com/a/12660523/4563358 delegate是将需要处理交给自己的代理. 在自己的对应的类中.h文件中申明对应的delegate @class CSPopMenu; @protocol CSPopMenuDelegate <NSObject> @optional -(void)PopMenuDimiss:(CSPopMenu *)popMenu; @end 插入一个可选择的方法,定义一个协议. @

iOS -- UIScrollView Delegate 代理

#import "MJViewController.h"@interface MJViewController () <UIScrollViewDelegate>@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@property (weak, nonatomic) IBOutlet UIImageView *minionView;@end@implementation MJViewControlle

UIApplication和delegate代理

所有的移动操作系统都有个致命的缺点:app很容易受到打扰,比如一个来电或者锁屏都会导致app进入后台甚至被终止 还有很多其他类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这是UIApplication会通知他的delegate对象 让delegate对象来处理这些系统事件 常用的代理方法 @implementation AppDelegate /** * 程序启动完毕就会调用 */ - (BOOL)application:(UIApplication *)applica

iOS中delegate代理对象使用weak和assign哪个?

在定义delegate对象的时候,我们经常看到可以使用weak 和 assign 但是到底哪个使用更合适呢? 首先:使用assign,声明的对象即使对象销毁了,但是指针依然存在,这样造成声明的对象变成了野指针,这样导致内存泄露. 而使用 weak ,当对象销毁时,即将delegate = nil,这样不会产生野指针这种情况出现. 所以答案很明显 我们在定义代理对象的时候,通常使用weak来修饰.

delegate代理补充

一. 协议:一对方法的声明 二.具体的步骤 1,定义协议 2, 给外界提供设置delegate属性的接口(在该类的.h文件中) 在定义delegate属性的时候,需要使用assign属性,防止循环引用. 3, 在外界给delegate赋值 4,外界接受协议 可以添加在外界 .m文件自动生成的类目后. 4,在对应的时间点让delegate执行协议中对应的方法(在该类的 .m 文件中) 5,外界实现协议的方法