什么是object c 的 delegate
http://blog.csdn.net/flamboyant/article/details/7464142
个人理解:
1. delegate是一种设计模式或者设计思想,从object c层面看,没有直接支持。
2. delegate的使用是利用 @protocol 方式来申明的
3. AViewController使用delegate, .h文件中申明遵守这个delegate;同时在.m文件中实现这个delegate方法
4. BViewController定义: NSObject<UIViewPassValueDelegate> * delegate;可以看出BViewController定义和实现的delegate实际上
就是一个遵循某个protocol的类的指针。 BViewController.m中可以直接调用delegate的方法:[delegate xxx] xxx就是在@protocol里定义的抽象方法,BViewController里不需要实现此方法。
5.在3中建立AViewController和BViewController的关联:[BViewController.delegate = self], self就是AViewController
6.通过3/4/5的实现,一旦BViewController里有事件发生,AViewController就会监听到,然后具体的事件处理是在AViewController里实现的
7. 用这种方法很好的实现了AViewController和BViewController的交互,他们除了delegate的接口,没有任何其他的关联,达到了很好的解耦效果
后面是一些参考,作为备忘。
参考:http://blog.sina.com.cn/s/blog_6602ffbc01011gcf.html
首先,大家应该都明白的是委托是协议的一种,顾名思义,就是委托他人帮自己去做什么事。也就是当自己做什么事情不方便的时候,就可以建立一个委托,这样就可以委托他人帮自己去实现什么方法。
其次,我简单的总结了一下自己用到的委托的作用有两个,一个是传值,一个是传事件。
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.
A 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 as alertView: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.
阅读(748) | 评论(0) | 转发(0) |