什么是object c 的 delegate
来源:互联网 发布:淘宝客劫持最新版 编辑:程序博客网 时间:2024/05/21 00:18
个人理解:
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
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.
- 什么是object c 的 delegate
- 理解object-c 中的delegate
- [object-c基础]delegate机制
- Object-C delegate 设计模式
- object-c:委托(delegate)
- object-c中delegate使用简单方法
- Object C学习笔记16-委托(delegate)
- Object C学习笔记16-委托(delegate)
- [C#-2] delegate和Delegate的区别
- [C#] 对delegate的理解
- 什么是委托(Delegate)
- 什么是委托(delegate)
- 什么是委托(Delegate)
- 什么是委托(Delegate)
- 什么是object?
- 什么是object?
- 什么是Object
- 什么是Qt的代理QItemDelegate_Spin Box Delegate Example例子分析
- JasperException: is quoted with " which must be escaped when used within the value
- Exercises 3-5
- jsp/Servlet 服务器服务程序:Servlet---->service()
- Android开发调试时logcat不显示问题
- 一步一步走进字符驱动--认识内核模块
- 什么是object c 的 delegate
- 精通JAVA的25个标准
- Android之Intent的几种用法<总结>
- Windows Phone开发学习之七——IDE介绍
- C++连接Mysql
- 匿名内部类
- nginx学习总结五(nginx反向代理)
- Curl vs Wget
- c++复习 十一(100~10000内的回文数)