如何在iPhone中使用多个代表?

时间:2011-10-28 12:51:34

标签: iphone json

我使用下面的代码来使用json,但我需要在同一页面中更多的url连接,如何实现它,提前感谢

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}

- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];

}

-(void)searchForStuff:(NSString *)text
 {
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL   URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

我使用php进行网络访问

4 个答案:

答案 0 :(得分:2)

您可以使用实例变量来保持指向连接的指针。然后在委托回调中,检查指针相等性以检查您正在处理的连接。

答案 1 :(得分:1)

当NSValue符合NSCopying时,我用它来包装指向连接的指针,并使用它作为访问NSMutableDictionary相关数据的密钥。例如,您可能会执行以下操作:

-(void)searchForStuff:(NSString *)text withTarget:(id)target selector:(SEL)selector {
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:target,@"target",NSStringFromSelector(selector),@"selector",nil];
    NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [myMutableDictionary setObject:options forKey:[NSValue valueWithPointer:c]];
    [c release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSValue *p = [NSValue valueWithPointer:connection];
    NSDictionary *options = [myMutableDictionary objectForKey:p];
    if (options) {
        id target = [options objectForKey:@"target"];
        SEL selector = NSSelectorFromString([options objectForKey:@"selector"]);
        if (target && selector && [target respondsToSelector:selector]) {
            [target performSelector:selector withObject:responseData];
        }
    }
}

答案 2 :(得分:0)

不要做任何事情。

而是使用辉煌的ASIHTTPRequest库,这使得一切变得更简单和更好。从字面上看,自从我几年前发现ASI以来,我没有写过一个NSURLConnection,而不是一个。

ASI的块接口允许您在激活请求对象及其处理程序代码之前配置它,并且不需要委派。

__block ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myNSURLObject];
[r setCompletionBlock:^{
    NSLog([r responseString]); //for instance
}];
[r startAsynchronous];

如果块吓到你,你也可以在特定方法中指出特定请求,因此可以单独处理不同的请求类型:

- (void) viewDidLoad { //or wherever
    ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myFirstURL];
    r.delegate = self;
    [r setDidFinishSelector:@selector(requestDone:)];
    [r startAsynchronous];
}

// then later on...
- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}

答案 3 :(得分:0)

您实际上不需要多个代表。您需要多个NSURLConnection,并且可以测试以查看哪个正在调用委托方法。

例如。假设以下实例变量(或属性):

NSURLConnection *connectionA;
NSURLConnection *connectionB;
NSMutableData *dataA;
NSMutalbeData *dataB;

首先,实例化每个NSURLConnection变量

-(void)searchA:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.a.com/%@", text]]];
    connectionA = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void)searchB:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.b.com/%@", text]]];
    connectionB = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

然后,您可以测试以查看哪个连接正在调用委托方法,并根据连接自定义实现

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection == connectionA) {
        [dataA appendData:data];
    }
    else if (connection == connectionB) {
        [dataB appendData:data];
    }
}

您需要为每个委托方法执行此操作。

相关问题