butter_baby
butter_baby

Reputation: 888

Storyboard UILabel won't update text.

I am having an issue of trying to update my UILabel that is connected via storyboard. When I log my data out it shows up in the console just fine, but my label doesn't update as it should:

CZWundergroundRequest *request = [CZWundergroundRequest newConditionsRequest];
    request.location = [CZWeatherLocation locationFromCoordinate: currentLocation];
    request.key = @"";
    [request sendWithCompletion:^(CZWeatherData *data, NSError *error) {
        self.currentTemperatureLabel.text = [NSString stringWithFormat:@"%f", data.current.temperature.f];
        NSLog(@"%f", data.current.temperature.f);
    }];

When I update the label in ViewDidLoad it works just fine, but once I try to send data to it, it doesn't update. I tried removing the label and reattaching the outlet too. Any ideas?

Upvotes: 1

Views: 224

Answers (1)

Nicholas Smith
Nicholas Smith

Reputation: 11754

You can only update UI elements when you're on the main thread, that request will likely be running on a background thread to prevent from blocking the UI.

If you wrap the call to set the label in a dispatch_async on the main thread like below it should solve your problem.

dispatch_async(dispatch_get_main_queue(), ^{ self.currentTemperatureLabel.text = [NSString stringWithFormat:@"%f", data.current.temperature.f]; });

Upvotes: 4

Related Questions