我正在构建一个处理 NSURLConnection 请求的类。为了允许其他类使用这个类,我想允许主类在触发 connectionDidFinishLoading 时调用委托。
我查看了很多文档,但找不到任何给出明确示例的内容,并且由于某种原因,我拥有的代码没有调用委托。我到目前为止的代码是(代码不相关已删除):
界面:
@interface PDUrlHandler : NSObject {
id delegate;
}
- (void)searchForItemNamed:(NSString *)searchQuery;
@property (nonatomic, assign) id delegate;
@end
@interface NSObject (PDUrlHandlerDelegate)
- (void)urlHandler:(PDUrlhandler*)urlHandler searchResultsFinishedLoading:(NSDictionary *)resultData;
@end
执行:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Fininshed Loading...");
resultData = [self parseJSON:jsonData];
if(delegate && [delegate respondsToSelector:@selector(urlHandler:searchResultsFinishedLoading:)]) {
NSLog(@"Delegating!");
[delegate urlHandler:self searchResultsFinishedLoading:resultData];
} else {
NSLog(@"Not Delegating. I dont know why.");
}
}
另一个类中的委托:
- (void)urlHandler:(PDUrlhandler*)urlHandler searchResultsFinishedLoading:(NSDictionary *)resultData;
{
NSLog(@"Delegating!!!!");
}