我正在尝试异步加载Web内容。一旦调用connectionDidFinishLoading方法,我不确定如何更新视图控制器中的标签/其他内容。在下面的示例中,我只是尝试更新标签以显示内容已加载。我该怎么做?谢谢!
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[responseData
length]);
NSString *txt = [[NSString alloc] initWithData:responseData encoding:
NSASCIIStringEncoding];
label.text = @"DISPLAY THIS WHEN FINISHED";
}
我被告知让我的viewController成为NSURLConnectionDelegate,然后从viewDidLoad运行fetchData方法,然后在connectionDidFinishLoading中获取数据时使用您获取数据的数据。谁知道从哪里开始?谢谢!
答案 0 :(得分:1)
正如Ramy正确指出的那样,你必须在主线程上更新你的UI,然后:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %d bytes of data",[responseData length]);
NSString *txt = [[NSString alloc] initWithData:responseData
encoding: NSASCIIStringEncoding];
NSString *text = @"DISPLAY THIS WHEN FINISHED";
[label performSelectorOnMainThread:@selector(setText:) withObject:text waitUntilDone:NO]
}