简体   繁体   English

调用connectionDidFinishLoading后更新View Controller

[英]Update View Controller After connectionDidFinishLoading is Called

I am trying to load web content asynchronously. 我正在尝试异步加载Web内容。 I am not sure how to update labels/other content in my view controller once the connectionDidFinishLoading method is called. 调用connectionDidFinishLoading方法后,我不确定如何在视图控制器中更新标签/其他内容。 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";  
}

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. 我被告知让我的viewController成为NSURLConnectionDelegate,然后从viewDidLoad运行fetchData方法,然后使用在connectionDidFinishLoading中获取数据时为我们获取的数据。 Anyone know where to begin? 有人知道从哪里开始吗? Thanks! 谢谢!

As Ramy correctly pointed you must update your UI on the main thread, then : 正如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]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM