respondsToSelector总是返回false

时间:2014-09-23 17:25:54

标签: ios objective-c

我有以下四个文件,

HttpClient.h

#import <Foundation/Foundation.h>


@interface HttpClient : NSObject{
    ....
}

   ....
    @end

@protocol HttpClientDelegate <NSObject>

@required

- (void)handleReceivedData: (NSArray*)results returnArrayOrDic:(NSNumber *)returnArrayOrDic;
- (void)handleConnectionError: (NSURLConnection *)connection error:(NSError*)error;
@end

HttpClient.m

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"*********************************************");
    ....
    NSLog(@"delegate in callee:%@", passedInDelegate);
    if (passedInDelegate == nil) NSLog(@"passedInDelegate is NIL");
    BOOL flag = [passedInDelegate respondsToSelector:@selector(handleReceivedData:)];
    NSLog(flag ? @"Yes" : @"No");
    flag = [passedInDelegate respondsToSelector:@selector(handleReceivedData)];
    NSLog(flag ? @"Yes" : @"No");
    if([passedInDelegate respondsToSelector:@selector(handleReceivedData:)]){
       NSLog(@"handleReceivedData is founded");
      [passedInDelegate performSelector:@selector(handleReceivedData:) withObject:jsonObject withObject:returnArrayOrDic];
    }

    NSLog(@"connectionDidFinishLoading");

}

ProductListViewController.h

@interface ProductListViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,HttpClientDelegate>
 ....

/*this is handle the call back*/

- (void)handleReceivedData: (NSArray*)results returnArrayOrDic:(NSNumber *)returnArrayOrDic;
- (void)handleConnectionError: (NSURLConnection *)connection error:(NSError*)error;

@end

ProductListViewController.m

/*callback of the httpClient*/

- (void)handleReceivedData: (NSArray*)results returnArrayOrDic:(NSNumber *)returnArrayOrDic{
    NSLog(@"********************");
    NSLog(@"handleReceivedData has been called");
    ....
}

从打印出来的日志中

 - 2014-09-24 01:09:20.913 [298:60b] delegate in callee:<ProductListViewController: 0x1467d860> 
 - 2014-09-24 01:09:20.914 [298:60b] No     
 - 2014-09-24 01:09:20.915 [298:60b] No

passedInDelegate不是nil并按预期分配给ProductListViewController。但是,当从HttpClient回调到ProductListViewController时,[passInDelegate respondsToSelector:@selector(handleReceivedData :)]和[passInDelegate respondsToSelector:@selector(handleReceivedData)]都是NO。

感谢您的评论吗?

此致 锤

1 个答案:

答案 0 :(得分:2)

相关方法的名称为handleReceivedData:returnArrayOrDic:,而非handleReceivedData:

@selector(handleReceivedData:)的所有引用更改为@selector(handleReceivedData:returnArrayOrDic:)

你的整个方法太复杂了。只是做:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"*********************************************");
    ....
    NSLog(@"delegate in callee:%@", passedInDelegate);
    if ([passedInDelegate respondsToSelector:@selector(handleReceivedData:returnArrayOrDic:)]){
        NSLog(@"handleReceivedData is founded");
        [passedInDelegate handleReceivedData:jsonObject returnArrayOrDic:returnArrayOrDic];
    }

    NSLog(@"connectionDidFinishLoading");
}

无需使用performSelector:withObject:withObject:。只需直接调用该方法即可。