简体   繁体   中英

Update View Controller After connectionDidFinishLoading is Called

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";  
}

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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