现在,我的视图控制器正在做一些非常简单的事情: 呼叫Web服务 打印一个值 将UILabel的文本值设置为该值 1 2非常活泼.第三步需要大约10秒才能在模拟器中显示在屏幕上. switch currentTemp
>呼叫Web服务
>打印一个值
>将UILabel的文本值设置为该值
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)" }
知道为什么这么慢吗?在打印值时,是否还需要执行其他操作才能使更改出现?
您发布的代码中没有太多内容,但听起来您在Web服务回调中直接进行标签更新,这通常在后台线程上运行.所有UI工作都需要在主线程上完成,否则你将遇到这样的问题.如果是这样,使用GCD的dispatch_async在主线程上运行该switch语句将解决您的问题: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)" } }