0

我必须上课 A 和 B 。我的 B 类具有与服务器连接的方法。我从类 AI 调用此方法已实现 NSURLConnection.class A 的委托,该委托具有检查连接功能,其返回类型为 BOOL 。我从这个方法调用B类的ping方法如下:A类:

-(BOOL)checkConnectivity:(Server *)newServer
{
    [b ping];
    return FALSE;
}

B类:

-(void)ping
{


    NSURL *url=[[NSURL alloc]initWithString:@"my url"];
    request=[NSMutableURLRequest requestWithURL:url];
    connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(connection)
    {
        webData=[NSMutableData data];
    }
}


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


-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Finished loading");   
}

网址没有问题。那怎么了?为什么委托方法没有调用?

4

1 回答 1

0

检查你是否添加了这个:

@interface yourViewController : UIViewController <NSURLConnectionDelegate>

现在在您的 ping 方法中执行此操作:

 NSURLConnection * connection = [[NSURLConnection alloc] 
                            initWithRequest:request
                                   delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                  forMode:NSDefaultRunLoopMode];
 [connection start]
于 2012-08-17T04:50:10.593 回答