代表是什么意思?为什么我们需要它?
可能的重复:
代表们,我无法理解他们
嗨朋友们,
什么目标C中的委托是什么意思?为什么我们需要它?我们什么时候应该使用它?里面有什么类型吗?如何使用?
请朋友们用简单的文字和例子来解释一下。我看了很多文章,论坛..但我仍然听不懂它的确切解释..
谢谢
Possible Duplicate:
Delegates, can't get my head around them
Hi friends,
What is mean by delegate in objective C? Why we need it? When should we use it? Is there any types in it? How to use it?
Please friends, use simple words and examples to explain. I saw so many articles, forums.. But still i can not catch the exact explanation of it..
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
委托是一种修改类行为而不需要对类进行子类化的方法。通常,您不想显着改变行为,而是稍微调整一下;子类化太过分了,所以这就是代表发挥作用的地方。
这样看:一个青少年代表一个阶级,她的父母是代表。少女的朋友打电话让她去商场逛逛,但少女必须先询问她的父母是否可以。家长——代表——可以说“是”或“否”。这就是 Cocoa 中代表的工作方式。
代表通常可以是任何类型。在 10.6 中,许多委托通过可选方法实现协议,因此您会看到类似
id
的类型,但这在 10.6 之前并不常见。A delegate is a way to modify the behavior of a class without requiring the class to be subclassed. Often you don't want to dramatically change behavior, but tweak it a bit; subclassing would be overkill, so that's where delegates come in to play.
Look at it this way: a teenager represents a class, and her parent the delegate. The teenager's friend calls her to come hang out at the mall, but the teenager has to ask her parents if it's okay first. The parent -- the delegate -- can say yes or no. That's how delegates work in Cocoa.
Delegates can generally be of any type. In 10.6, many delegates implement protocols with optional methods, so you'll see types like
id <BlahClassDelegate>
, but that wasn't common before 10.6.