当您使用connectionWithRequest
时,UI不应锁定。尝试建立在你的UI标签,让你的连接与数据的电流量更新它,就像这样:
- (void)downloadContentFromUrl:(NSURL *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
receivedData = [[NSMutableData data] retain];
self.downloadProgressLabel.text = @"Downloading...";
} else {
// oh noes!
}
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
int kb = [receivedData length]/1024;
self.downloadProgressLabel.text = [NSString stringWithFormat:@"Downloaded\n%d kB", kb];
}
connectionWithRequest
确实在它自己的线程中运行 - 无需您担心这一点。实际上它必须从主线程开始。查看NSUrlConnection文档了解更多信息。
你用哪种方法执行该调用? – David 2010-08-10 08:46:34
我在根视图控制器的子视图(不是表格)上调用它。 – emenegro 2010-08-10 09:56:16
你应该包含更多的代码。不知道何时以及如何初始化您的连接,很难排除故障。 – thomax 2010-08-10 10:16:21