有人可以解释一下 Objective-C 中的代表吗?

发布于 2024-09-30 19:35:52 字数 155 浏览 1 评论 0原文

我已经使用 Objective-C 一段时间了,并且非常了解它的大部分功能。然而,代表的概念让我困惑。 有人可以对委托是什么、它们在 iPhone SDK 中的使用方式以及我如何在自己的代码中最好地利用它们给出一个简洁且易于理解的解释吗?

谢谢!

I have been using Objective-C for a while and pretty much understand most of its features. However, the concept of delegates eludes me. Can someone please give a succinct and easy to comprehend explanation of what delegates are, how they are used in the iPhone SDK, and how I can best make use of them in my own code?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪是无色的血 2024-10-07 19:35:52

在 Objective-C 中使用委托有几个略有不同的主要原因:

  1. 增强框架类的基本功能。例如,UITableView 本身非常无聊,因此您可以给它一个委托来处理有趣的部分(创建表格单元格,向节标题添加文本,等等)。这样,UITableView 永远不会改变,但不同的表视图的外观和行为可能非常不同。

  2. 与依赖层次结构中的父对象通信。例如,您可能有一个带有按钮的视图,用户可以按下该按钮来执行影响其他视图的操作。视图必须向其父视图或视图控制器发送消息,以便它可以创建、销毁或修改其他视图。为此,您需要将父对象作为弱引用(在 Objective-C 中,一个分配属性)传递到您的视图中,很可能通过协议。然后,视图可以将协议中声明的任何消息发送到父对象或委托对象。

这种方法不需要涉及视图。例如,NSURLConnection 使用此机制将事件传递回其委托,该委托可能是创建它的对象。

There are a couple main reasons to use delegates in Objective-C, which are subtly different:

  1. Enhancing the base functionality of a framework class. For example, a UITableView is pretty boring on its own, so you can give it a delegate to handle the interesting bits (creating table cells, adding text to section headers, what have you). This way, UITableView never changes, but different table views can look and act very differently.

  2. Communicating to parent objects in your dependency hierarchy. For example, you may have a view with a button that the user may push to do something that affects other views. The view will have to send a message to its parent view, or perhaps the view controller, so that it can create or destroy or modify other views. To do this you'd pass the parent object into your view, most likely through a protocol, as a weak reference (in Objective-C, an assign property). The view could then send any message declared in the protocol to the parent, or delegate, object.

This approach need not involve views. For example NSURLConnection passes event back to its delegate, which may be the object that created it, using this mechanism.

不必你懂 2024-10-07 19:35:52

本质上,委托就是一个接受来自另一个对象的反馈的对象。简而言之,当某个对象发生问题时,它会告诉其委托(假设它有一个委托)。

例如,假设我有一个 UIViewController,其中 UITextView 放置在视图中间。我将 UIViewController 设置为 UITextView 的委托。然后,当在文本视图上执行某些操作(开始编辑、文本更改、结束编辑等)时,它会告诉它的委托,以便它可以执行它需要执行的任何逻辑,例如每次字符更改时进行拼写检查,或忽略键盘收到返回键按下时。

委托方法执行与 C 中的回调函数类似的功能。

希望这是有道理的:)

Essentially, all a delegate is, is an object that accepts feedback from another object. Put simply, when stuff happens to an object, it tells its delegate (assuming it has one).

For instance, lets say I have a UIViewController with a UITextView placed in the middle of the view. I set up my UIViewController to be the delegate of the UITextView. Then, when certain actions are performed on the text view (begin editing, text changes, end editing, etc), it tells it's delegate so it can do whatever logic it needs to do, like spell checking every time characters change, or dismissing the keyboard when it receives a return key press.

Delegate methods perform a similar function to callback functions in C.

Hope that makes sense :)

诗化ㄋ丶相逢 2024-10-07 19:35:52

我从 Lynda.com 教程中得到的最好且简单的概念是:当您设置委托时,这意味着您已被分配工作要做。因此,如果您想使用协议方法中编写的方法,则必须通过在委托类参考中搜索并使用它们来实现它们。我希望它有帮助。

顺便说一句,代表们都很优秀。他们是你的朋友。它们旨在让您作为程序员的生活变得更加轻松。

Best and simple concept I got from a Lynda.com Tutorial was: When you set a Delegate it means you have been given work to do. So, if you want to use methods that are written in a protocol method, you must implement them by searching in the Delegate Class Reference and using them. I hope it helped.

By the way, Delegates are excellents. They are your friends. They have been made to make your life as a programmer much easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文