委托,一些代码示例?对象如何委托给其他对象
我想对代表团有更好的了解。有人可以粘贴一个好的委托代码示例并解释它是如何工作的吗?
I would like to gain a better understanding about the delegation. Can somebody please paste a good code sample of delegation and explain how it works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个很好的例子: http://en.wikipedia.org/wiki/ delegate_pattern#Objective-C_example
在此示例中,
MyCoolAppController
创建TCScrollView
类型的对象,并设置TCScrollView
的“delegate”属性代码> 到<代码>自我。这意味着当 TCScrollView 调用时,它会要求 MyCoolAppController(TCScrollView 的委托)执行一些计算并查看是否可以滚动。你可以说“
MyCoolAppController
is the delegate ofTCScrollView
”来描述这一点;TCScrollView
要求MyCoolAppController
代表它做一些工作。There is a pretty good example at: http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example
In this example,
MyCoolAppController
creates and object of typeTCScrollView
, and sets the "delegate" property of theTCScrollView
toself
. This means that when the TCScrollView callsit is asking the
MyCoolAppController
(the delegate of the TCScrollView) to perform some calculations and see if it is ok to scroll. You can say "MyCoolAppController
is the delegate ofTCScrollView
" to describe this;TCScrollView
asksMyCoolAppController
to do some work on its behalf.您是指 .NET、Java 还是其他语言委托?
Do you mean .NET or Java or some other language delegate?
.NET 术语中的委托只不过是一个函数指针,或者换句话说,是一个指向可执行代码块的变量。它们可以以多种方式使用。一种方法是在事件上下文中使用它们。假设您有一个 ASP.NET 页面,并且正在使用 MVP(该页面上的模型视图演示者模式)。您希望演示者收到视图上保存按钮的单击事件的通知。您可以在视图接口上定义一个事件,但为了订阅该事件并对其采取操作,您需要注册一个在引发事件时触发的方法。例如:
A delegate in .NET parlance is nothing more than a function pointer, or in other words a variable that points to a block of executable code. They can be used in may ways. One way is to use them in the context of events. Lets say you have an ASP.NET page and you are using the MVP (Model View Presenter pattern on that page). You want your presenter to be notified of the click event of the save button on the view. You can define an event on the views interface, but in order to subscribe to that event and to take action on it you need to register a method that gets fired when the event is raised. For example:
这是我写的解释委托的答案:https://stackoverflow.com/questions/1089737#1090170
Here's an answer I wrote explaining delegation: https://stackoverflow.com/questions/1089737#1090170
委托是响应事件的一种方式。在其他语言中,您可能会通过子类化来做到这一点。例如,假设您有一个表视图。您可以子类化 tableview 并覆盖 tableView:didSelectRowAtIndexPath: 方法,但这会变得混乱并创建不必要的子类(以及它不可重用的事实)相反,您创建一个 TableViewDelegate 类并告诉您的表视图(tableView 。代表)。这样,当有事情发生时,该方法就会自动被调用。这是一个非常干净的事件处理解决方案。
当你编写了一些涉及委托的应用程序(表视图是最重要的)后,你就会掌握它的窍门。
A delegate is a way to respond to events. In other languages you would probably do this by subclassing. For example, say you have a table view. You could subclass the tableview and override the tableView:didSelectRowAtIndexPath: method, but that would get messy and create an unnecessary subclass (along with the fact that its not reusable) Instead, you create a TableViewDelegate class and tell your table view about it (tableView.delegate). This way, the method will automatically get called when something happens. This is a really clean solution to event-handling.
After you write a few apps that involve delegates (table views are the big ones), you'll get the hang of it.