更新UILabel.text很慢

时间:2014-08-21 19:53:34

标签: ios swift

现在,我的视图控制器正在做一些非常简单的事情:

  1. 致电网络服务
  2. 打印一个值
  3. 将UILabel的文本值设置为该值
  4. 1& 2非常活泼。第三步需要大约10秒才能在模拟器中显示在屏幕上。

            switch currentTemp {
        case 80..<180:
            self.riskType.text = "Heat Index:"
            self.perceivedTemperatureValue.text = "\(currentHeatIndex)"
        case -100..<50:
            self.riskType.text = "Wind Chill:"
            self.perceivedTemperatureValue.text = "\(currentWindChill)"
        default:
            self.riskType.text = "Temperature:"
            println(currentTemp)
            self.perceivedTemperatureValue.text = "\(currentTemp)"
        }
    

    知道为什么这么慢?还有什么我需要做的就是在我打印出值后立即显示更改吗?

1 个答案:

答案 0 :(得分:7)

您发布的代码中并没有很多内容,但听起来您直接在Web服务回调中进行标签更新,这通常在后台线程上运行。 所有UI工作都需要在主线程上完成,否则您将遇到此类问题。如果是这样,请使用GCD&#39在主线程上运行switch语句; dispatch_async将解决您的问题:

dispatch_async(dispatch_get_main_queue()) {
    switch currentTemp {
    case 80..<180:
        self.riskType.text = "Heat Index:"
        self.perceivedTemperatureValue.text = "\(currentHeatIndex)"
    case -100..<50:
        self.riskType.text = "Wind Chill:"
        self.perceivedTemperatureValue.text = "\(currentWindChill)"
    default:
        self.riskType.text = "Temperature:"
        println(currentTemp)
        self.perceivedTemperatureValue.text = "\(currentTemp)"
    }
}