代表们,不能让我的头靠近他们 [英] Delegates, can't get my head around them

查看:151
本文介绍了代表们,不能让我的头靠近他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我正在寻找有关代表的有用资源。我明白代表在后台坐下,并在某些事情发生时收到消息 - 例如选择表格单元格,或者检索来自网络上的连接的数据。



我想知道的是如何使用多个对象的代理。据我所知,为对象(例如表单元格)指定相同的委托将导致同时为这两个单元调用相同的事件。有什么相当于为特定对象实例化代理?



提前感谢!

解决方案

在Cocoa中,当调用委托方法时,对象几乎总是识别自己。例如,当调用它时,UITableView将自己作为委托消息的第一个参数传递:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :( NSIndexPath *)indexPath 

如果您希望同一个委托来处理多个UITableView,那么你只需要一些有条件的 tableView 对象传递给方法:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :( NSIndexPath *)indexPath 
{
if(tableView == self.myFirstTableView){
// do stuff
} else if(tableView == self.mySecondtableView){
//做其他东西
}
}

}



如果不想直接比较对象指针,可以随时使用标签属性以唯一标识您的视图。


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?

Thanks in advance!

解决方案

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

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
    }
}

}

If you don't want to compare the object pointers directly, you can always use the tag property to uniquely identify your views.

这篇关于代表们,不能让我的头靠近他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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