简体   繁体   English

区分对相同委托方法的调用

[英]Differentiate between calls to the same delegate method

Let's imagine I have a method: -[myClass getDataForUser:user] and when it is done it calls a delegate's method gotData: and the results appear on my main class. 假设我有一个方法: -[myClass getDataForUser:user] ,完成后它将调用委托的方法gotData:结果将出现在我的主类中。 If I do two calls to getDataForUser: , for example: 如果我两次调用getDataForUser:例如:

[myClass getDataForUser:@"user1"];
[myClass getDataForUser:@"user2"];

how can I differentiate between these two calls in the delegate method? 如何在委托方法中区分这两个调用?

EDIT: 编辑:

The library is MGTwitterEngine, the two calls I make are -[_engine getDirectMessagesSinceID:1 startingAtPage:1]; 该库是MGTwitterEngine,我进行的两个调用是-[_engine getDirectMessagesSinceID:1 startingAtPage:1]; and [_engine getSentDirectMessagesSinceID:1 startingAtPage:1]; [_engine getSentDirectMessagesSinceID:1 startingAtPage:1]; and the delegate method is directMessagesReceived:forRequest: 委托方法是directMessagesReceived:forRequest:

Use an instance of MGTwitterEngine for each call, and give each instance a different delegate object. 为每个调用使用MGTwitterEngine实例,并为每个实例指定不同的委托对象。 Inside each of these delegate objects, you can store the user id. 在每个委托对象中,您可以存储用户ID。 Set these delegate objects so they can accept your original class as their delegates. 设置这些委托对象,以便它们可以接受原始类作为委托。 Then you can use delegate gotData:(id)data forUser:(NSString *)user finally. 然后,您可以最终将委托getData:(id)data用于User:(NSString *)user。 Not pretty, but may work. 不漂亮,但是可以用。

Add an argument to the delegate's method: 在委托的方法中添加一个参数:

[delegate gotData:(id)data forUser:(NSString *)user]

There are other ways, but not nearly as clean and easy 还有其他方法,但还不如干净和容易

Edit: OK, it's in a library so it can't be changed. 编辑:确定,它在库中,因此无法更改。 However, the delegate does have a for[something] argument, in this case forRequest . 但是,委托确实有一个for[something]参数,在本例中为forRequest So, all you need to do is find a way to associate a request with a username. 因此,您所需要做的就是找到一种将请求与用户名关联的方法。 I suggest putting them in a map, mapping request to user . 我建议将它们放在地图中,将request映射到user Or, if you only have the two, you could just make a couple variables request1, request2 and user1, user2 and store the request and user in the appropriate variables, checking for which id goes with which name (in the delegate): 或者,如果只有两个,则可以创建几个变量request1, request2user1, user2 ,并将请求和用户存储在适当的变量中,检查哪个id与哪个名称(在委托中)相符:

if(thisRequest == request1) {
    thisUser = user1; 
} else {
    thisUser = user2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM