委托如何在 Objective-C 中工作? [英] How does a delegate work in objective-C?

查看:36
本文介绍了委托如何在 Objective-C 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 有谁知道我在哪里可以找到关于应用程序委托在 objective-C 中的工作方式和工作方式的很好的解释/教程?
  2. 我所拥有的两本书对 delegates 的阐述不够深入,也没有很好地解释它们,让我无法真正了解它们的力量和功能.
  1. Does anyone know where I can find a good explanation/tutorial of what and how an application delegate works in objective-C?
  2. The two books I have don't dwell on delegates enough and do not explain them very well for me to truly understand their power and function.

推荐答案

如有疑问,请查看 文档

基本上,委托是一种允许对象相互交互的方式,而不会在它们之间创建强大的相互依赖性,因为这会降低应用程序的设计灵活性.不是对象控制彼此,它们可以有一个委托,它们可以向其发送(或委托)消息,委托会做任何事情,以响应并对该消息采取行动,然后通常将某些东西返回给另一个对象.

Basically, delegation is a way of allowing objects to interact with each other without creating strong interdependencies between them, since this makes the design of your application less flexible. Instead of objects controlling one another, they can have a delegate which they send (or delegate) messages to, and the delegate does whatever they do, in order to respond and act to this message, and then usually return something back to the other object.

委托也是子类化的更好选择.您不必创建自己的自定义类来稍微改变其他对象的行为方式或将数据传递给它们,委托允许对象向其委托发送消息以为它们工作,而无需创建子类以对它们进行细微更改其他对象.

Delegation is also a better alternative to subclassing. Instead of you having to create your own custom classes to slightly alter the way that other objects behave, or pass them data, delegation allows objects to send messages to their delegates to do work for them without the overhead of creating subclasses to make minor changes to other objects.

当然,委托的主要缺点是可用的委托方法取决于 Apple 工程师认为有用的内容以及他们期望人们需要的常见实现,这对您可以实现的目标施加了限制.虽然,正如 Quinn Taylor 所指出的,这是 Cocoa 框架特有的,因此并不适用于所有情况.

Of course, the main disadvantage of delegation is that the delegate methods available are dependent on what the Apple engineers foresee as being useful and what common implementations they expect people to need, which imposes a restriction on what you can achieve. Although, as Quinn Taylor pointed out, this is specific to the Cocoa frameworks and so doesn't apply in all situations.

如果委托是比子类化更好的选择,那么就采用它,因为它是管理代码和对象之间交互的更简洁的方式.

If delegation is an option over subclassing, then take it, because it's a much cleaner way to manage your code and interactions between objects.

这篇关于委托如何在 Objective-C 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆