Delegates, can#39;t get my head around them(代表们,我无法理解他们)
问题描述
您好,我正在寻找有关代表的有用资源.我了解代表坐在后台并在某些事情发生时接收消息 - 例如.选择表格单元格,或检索来自网络连接的数据.
Hey, I'm looking for useful resources about Delegates. I understand that the delegate sits in the background and receives messages when certain things happen - e.g. a table cell is selected, or data from a connection over the web is retrieved.
我特别想知道的是如何将委托与多个对象一起使用.据我所知,为对象(例如表格单元格)指定相同的委托会导致同时为两个单元格调用相同的事件.有什么相当于为特定对象实例化委托的方法吗?
What I'd like to know in particular is how to use delegates with multiple objects. As far as I know, specifying the same delegate for an object (e.g. table cell) would cause the same events to be called for both the cells at the same time. Is there anything equivalent to instantiating a delegate for a particular object?
提前致谢!
推荐答案
在 Cocoa 中,对象几乎总是在调用委托方法时标识自己.例如,UITableView 在调用时将自己作为委托消息的第一个参数传递:
In Cocoa, objects almost always identify themselves when calling a delegate method. For example, UITableView passes itself as the first parameter of the delegate message when calling it:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
如果您希望同一个委托处理多个 UITableView,那么您只需要对传递给方法的 tableView 对象设置一些条件:
If you wanted the same delegate to handle multiple UITableViews, then you just need a some conditional on the tableView object passed to the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.myFirstTableView) {
// do stuff
} else if (tableView == self.mySecondtableView) {
// do other stuff
}
}
}
如果您不想直接比较对象指针,您可以随时使用 tag 属性来唯一标识您的视图.
If you don't want to compare the object pointers directly, you can always use the tag property to uniquely identify your views.
这篇关于代表们,我无法理解他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:代表们,我无法理解他们
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
