【问题标题】:Storyboard UILabel won't update text.Storyboard UILabel 不会更新文本。
【发布时间】:2017-04-12 10:14:25
【问题描述】:

我在尝试更新通过情节提要连接的 UILabel 时遇到问题。当我将我的数据注销时,它会很好地显示在控制台中,但我的标签没有按应有的方式更新:

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

当我更新 ViewDidLoad 中的标签时,它工作得很好,但是一旦我尝试向它发送数据,它就不会更新。我也尝试取下标签并重新安装插座。有什么想法吗?

【问题讨论】:

  • 尝试在主线程中更新
  • 始终在主线程中更新 UI 元素。

标签: ios objective-c storyboard


【解决方案1】:

您只能在主线程上更新 UI 元素,该请求可能会在后台线程上运行以防止阻塞 UI。

如果您在main thread 上将标签设置为dispatch_async 的调用,如下所示,它应该可以解决您的问题。

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

【讨论】:

  • 天哪...我知道我不应该跳过关于线程的那一章。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多