有人能解释objective-c中的委托吗?

时间:2022-09-14 19:58:26

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?

我使用Objective-C已经有一段时间了,我非常理解它的大部分特性。然而,我不理解代表的概念。能不能给我一个简单易懂的解释,说明什么是委托,如何在iPhone SDK中使用委托,以及如何在我自己的代码中最好地使用委托?

Thank you!

谢谢你!

3 个解决方案

#1


5  

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

在Objective-C中使用委托有几个主要原因,它们有细微的不同:

  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.

    增强框架类的基本功能。例如,UITableView本身就很无聊,所以您可以给它一个委托来处理有趣的位(创建表单元格,向节头添加文本,等等)。这样,UITableView就不会改变,但是不同的表视图的外观和行为会非常不同。

  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.

    在依赖关系层次结构中与父对象通信。例如,您可能有一个带有按钮的视图,用户可以按这个按钮来做影响其他视图的事情。视图必须向其父视图或视图控制器发送消息,以便它能够创建、销毁或修改其他视图。要做到这一点,您应该将父对象作为弱引用(在Objective-C中,是一个assign属性)传递到您的视图中,很可能是通过协议传递的。然后,视图可以将协议中声明的任何消息发送给父对象或委托对象。

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.

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

#2


3  

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.

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

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

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

Hope that makes sense :)

希望这是有道理的

#3


0  

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.

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

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

顺便说一下,各位代表都很出色。他们是你的朋友。它们让你的程序员生活更轻松。

#1


5  

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

在Objective-C中使用委托有几个主要原因,它们有细微的不同:

  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.

    增强框架类的基本功能。例如,UITableView本身就很无聊,所以您可以给它一个委托来处理有趣的位(创建表单元格,向节头添加文本,等等)。这样,UITableView就不会改变,但是不同的表视图的外观和行为会非常不同。

  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.

    在依赖关系层次结构中与父对象通信。例如,您可能有一个带有按钮的视图,用户可以按这个按钮来做影响其他视图的事情。视图必须向其父视图或视图控制器发送消息,以便它能够创建、销毁或修改其他视图。要做到这一点,您应该将父对象作为弱引用(在Objective-C中,是一个assign属性)传递到您的视图中,很可能是通过协议传递的。然后,视图可以将协议中声明的任何消息发送给父对象或委托对象。

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.

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

#2


3  

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.

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

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

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

Hope that makes sense :)

希望这是有道理的

#3


0  

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.

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

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

顺便说一下,各位代表都很出色。他们是你的朋友。它们让你的程序员生活更轻松。