调用connectionDidFinishLoading后更新View Controller [英] Update View Controller After connectionDidFinishLoading is Called

查看:66
本文介绍了调用connectionDidFinishLoading后更新View Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试异步加载Web内容.调用connectionDidFinishLoading方法后,我不确定如何在视图控制器中更新标签/其他内容.在下面的示例中,我只是尝试更新标签以显示内容已加载.我该怎么做?谢谢!

I am trying to load web content asynchronously. I am not sure how to update labels/other content in my view controller once the connectionDidFinishLoading method is called. In the sample below, I am just trying to update a label to show that the content has loaded. How would I do this? Thank you!

- (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中获取数据时为我们获取的数据.有人知道从哪里开始吗?谢谢!

I have been told to let my viewController be the NSURLConnectionDelegate and then to will run the fetchData method from the viewDidLoad and then use the data you get the data for us when it is fetched in the connectionDidFinishLoading. Anyone know where to begin? Thanks!

推荐答案

正如Ramy正确指出的那样,您必须在主线程上更新UI,然后:

As Ramy correctly pointed you must update your UI on the main thread, then :

- (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]
}

这篇关于调用connectionDidFinishLoading后更新View Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆