【问题标题】:Callback function in iOS using performSelectoriOS 中使用 performSelector 的回调函数
【发布时间】:2014-08-19 10:16:54
【问题描述】:

我正在尝试创建一个在 iOS 中调用被调用者的回调函数的方法。浏览动态函数调用后,这是我尝试过的:-

标题:-

+(void)url:(NSString *)url handler:(NSObject *)handler callback:(NSString *)callbackfn;

实现:-

+(void)url:(NSString *)url handler:(NSObject *)handler callback:(NSString *)callbackfn{
NSURLRequest *imageLoader = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

[NSURLConnection sendAsynchronousRequest:imageLoader queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    SEL s = NSSelectorFromString(callbackfn);
    if ([handler respondsToSelector:s]) {
        [handler performSelector:s];
    }else{
        NSLog(@"does not responds to handler %@",[handler self]);
    }


}];

}

调用它的 ViewController:-

-(IBAction)loadImage:(id)sender{

NSString *url = @"https://s3.amazonaws.com/uifaces/faces/twitter/ateneupopular/128.jpg";
[NetCall url:url handler:self callback:@"imageRec"];

}

-(void)imageRec{
    NSLog(@"net call done");
}

但是当代码执行时,它会在 LOG 中打印“不响应处理程序”。 我是否以错误的方式传递函数名称?

日志:-

2014-08-19 17:54:58.419 Testing[2194:78586] does not responds to handler <ViewController: 0x7b16c390>

【问题讨论】:

  • 你能发布显示的确切日志吗?
  • @jbouaziz:我已经更新了问题。
  • 您能评论if 语句并通过调用[handler performSelector:s]; 故意使应用程序崩溃吗?这个堆栈跟踪应该更有用。

标签: ios objective-c callback


【解决方案1】:

如果您尝试从异步请求中获取回调,我建议您使用块语法。它适用于 iOS4+。

+ (void)url:(NSString *)url callback:(void (^)(NSData *data))callback {
    NSURLRequest *imageLoader = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    [NSURLConnection sendAsynchronousRequest:imageLoader queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (callback)
            callback(data);
    }];
}

这就是你所说的:

// __weak typeof(self) weakSelf = self;  
NSString *stringUrl = @"https://s3.amazonaws.com/uifaces/faces/twitter/ateneupopular/128.jpg";  
[MyViewController stringUrl callback:^(NSData *data) {

    // Do something here with *data*
    // __strong typeof(weakSelf) strongSelf = weakSelf;
}];

如果您需要使用self,请使用弱引用,否则可能会保留强引用并可能导致您出现问题。 为了帮助你搭建积木,有这个网站http://goshdarnblocksyntax.com/


如果你仍然想使用选择器技术,这里是修改后的代码:

+ (void)url:(NSString *)url handler:(NSObject *)handler selector:(SEL)selector {
    NSURLRequest *imageLoader = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    [NSURLConnection sendAsynchronousRequest:imageLoader queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if ([handler respondsToSelector:selector]) {
            [handler performSelector:selector];
        }else{
            NSLog(@"does not responds to handler %@", handler);
        }

    }];
}

在你的 ViewController 中:

- (IBAction)loadImage:(id)sender {

    NSString *url = @"https://s3.amazonaws.com/uifaces/faces/twitter/ateneupopular/128.jpg";
    [NetCall url:url handler:self selector:@selector(imageRec)];
}

- (void)imageRec {
    NSLog(@"net call done");
}

【讨论】:

  • 嘿,使用块语法的回调有效。 respondsToSelector 仍然打印“不响应处理程序”。
  • 您是否在您的ViewController.h 文件中添加了- (void)imageRec;
  • @Tejas 如果你删除了@selector,你会得到一个 Xcode 警告。这就是我首先添加它的原因。
  • 但是当我在@selector里面输入时,它说“不响应处理程序”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2018-12-29
相关资源
最近更新 更多