我有两个 NSURLConnections。第二个取决于第一个的内容,因此处理从连接接收到的数据对于两个连接来说是不同的。
我刚开始学习 Objective-C,我想知道实现委托(delegate)的正确方法是什么。
现在我正在使用:
NSURL *url=[NSURL URLWithString:feedURL];
NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:urlR delegate:self];
我不想使用 self 作为委托(delegate),如何定义两个具有不同委托(delegate)的连接?
NSURLConnection *c1 = [[NSURLConnection alloc] initWithRequest:url delegate:handle1];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:handle2];
如何创建 handle1 和 handle2 作为实现?还是接口(interface)?我真的不明白你会怎么做。
任何帮助都会很棒。
谢谢, 布赖恩·吉安福卡罗
最佳答案
Ben,虽然您的信息很有帮助,但它并没有完全回答我提出的问题。
我终于想出了如何设置我自己的代表,这才是我真正想要的。
我是这样实现的:
@interface DownloadDelegate : NSObject
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
@end
@implementation DownloadDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
@end
我们像这样使用委托(delegate):
DownloadDelegate *dd = [DownloadDelegate alloc];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:dd];
希望这对处于相同位置的任何人有所帮助,并再次感谢 Ben 的帮助。
谢谢,
布赖恩·吉安福卡罗
关于ios - Objective-C 中的多个 NSURLConnection 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/203294/