简体   繁体   English

通过Objective-C选择器进行回调

[英]callbacks via objective-c selectors

I have a "BSjax" class that I wrote that lets me make async calls to our server to get json result sets, etc using the ASIHTTPRequest class. 我编写了一个“ BSjax”类,该类允许我使用ASIHTTPRequest类对服务器进行异步调用以获取json结果集,等等。 I set it up so that the BSjax class parses my server's json response, then passes control back to the calling view controller via this call: 我对其进行了设置,以便BSjax类解析服务器的json响应,然后通过此调用将控制权传递回调用视图控制器:

[[self delegate] performSelectorOnMainThread:@selector(bsRequestFinished:) withObject:self waitUntilDone:YES];

... where "bsRequestFinished" is the callback method in the calling view controller. ...其中“ bsRequestFinished”是调用视图控制器中的回调方法。 This all worked fine and well until I realized that some pages are going to need to make different types of requests... ie I'll want to do different types of things in that callback function depending on which type of request was made. 在我意识到某些页面将需要发出不同类型的请求之前,所有这些工作都很好并且很好……也就是说,我想根据发出的请求类型在该回调函数中执行不同类型的事情。

To me it seems like being able to pass different callback function names to my BSjax class would be the cleanest fix... but I'm having trouble (and am not even sure if it's possible) to pass in a variable that holds the callback function name and then replace the call above with something like this: 对我来说,似乎能够将不同的回调函数名称传递给我的BSjax类将是最干净的解决方法……但是我在传递一个包含回调的变量时遇到了麻烦(甚至不确定是否可能)函数名称,然后将上面的调用替换为以下内容:

[[self delegate] performSelectorOnMainThread:@selector(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];

... where "self.variableCallbackFunctionName" is set by the calling view controller when it calls BSjax to make a new request. ...其中,在调用BSjax发出新请求时,由调用视图控制器设置“ self.variableCallbackFunctionName”。

Is this even possible? 这有可能吗? If so, advisable? 如果可以,建议吗? If not, alternatives? 如果没有,替代方案?

EDIT: Note that whatever fix I arrive at will need to take into account the reality that this class is making async requests... so I need to make sure that the callback function processing is correctly tied to the specific requests... as I can't rely on FIFO processing sequence. 编辑:请注意,无论我做出什么修复,都需要考虑到此类正在发出异步请求的事实……所以我需要确保回调函数处理正确地绑定到了特定请求……就像我一样不能依赖FIFO处理顺序。

EDIT 2: Looking like passing in a SEL type variable is the way to go? 编辑2:看起来像传递SEL类型变量是要走的路? What I'm fighting with now is figuring out the syntax for saving it as a retained property of the BSjax class. 我现在要解决的问题是弄清楚将其保存为BSjax类的保留属性的语法。

You can pass around selectors as SEL values. 您可以将选择器作为SEL值传递。 For example: 例如:

SEL oneSelector = @selector(bsRequestFinished:);
SEL anotherselector = @selector(somethingElse:);
NSString *nameOfSelector = askUserForSelectorName();
SEL dynamicSelector = NSSelectorFromString(nameOfSelector);

So just make your variableCallbackFunctionName a SEL and it's even simpler than you were hoping for: 因此,只需将您的variableCallbackFunctionName设为SEL,它甚至比您期望的要简单:

[[self delegate] performSelectorOnMainThread:self.variableCallbackFunctionName withObject:self waitUntilDone:YES];

What type of variable is variBkecallbackfunctionname? variBkecallbackfunctionname是什么类型的变量? An NSString? NSString? If it is, you'll probably want something like this: 如果是这样,您可能会想要这样的东西:

[[self delegate] performSelectorOnMainThread:NSSelectorFromString(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];

I am at an iPad so I can't test this, but give it a try. 我在iPad上,因此无法测试,请尝试一下。

Edit: and if variableCallbackFunctionName isn't an nsstring, the easiest way might be to make it, if I understand your question. 编辑:如果variableCallbackFunctionName不是一个nsstring,如果我理解您的问题,最简单的方法可能是创建它。

Edit 2: docs for that are at http://developer.apple.com/mac/library/iPad/index.html#documentation/cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html%23//apple_ref/c/func/NSSelectorFromString 编辑2:的文档位于http://developer.apple.com/mac/library/iPad/index.html#documentation/cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html%23//apple_ref / C / FUNC / NSSelectorFromString

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

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