ios - Objective-C 中的多个 NSURLConnection 委托(delegate)

标签 ios objective-c macos cocoa

我有两个 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/

相关文章:

ios - 由于 CTFont Ref 导致的内存泄漏

ios - iOS 8 中的 UISearchBar - UI 位移

ios - 在 iOS 和 ARC 中查找哪个对象引用了另一个对象

objective-c - 在这个CGBitmapContextCreate中,为什么bytesPerRow是0?

macos - 在 IB 中的两个 nsbutton 之间设置响应者链

ios - UIView 图像中的缩放效果

ios - 如何将应用内购买添加到 iOS 应用程序?

iphone - CGContextClipToMask 不剪裁

macos - 无法在 OS X Mountain Lion 上安装 Mono

Movable Type 的 MySQL 驱动模块问题