繁体   English   中英

UILabel内容显示一秒钟,然后消失

[英]UILabel content is displayed for a second and then disappears

我正在尝试构建一个天气应用程序,其中我使用Alamofire获取天气数据。 数据获取工作正常,但是用于显示获取的值的UILabel仅显示数据一秒钟,之后数据消失。 标签不是表视图的一部分,它们位于屏幕顶部的UIView中的上方。 updateMainUI()是将标签分配给要在屏幕上显示的相应值的方法。 我已经研究了解决方案,他们建议使用Dispatch async,但是我无法解决这个看似简单的问题。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    tableView.dataSource = self
    tableView.delegate = self

    currentWeather = CurrentWeather()
    DispatchQueue.main.async {
        self.currentWeather.downloadWeatherDetails {
            self.updateMainUI()
        }
    }
}

updateMainUI()方法如下:

func updateMainUI() {
    dateLabel.text = currentWeather.date
    print(currentWeather.date)
    currentTempLabel.text = "\(currentWeather.currentTemp)"
    currentWeatherTypeLabel.text = currentWeather.weatherType
    locationLabel.text = currentWeather.cityName
    currentWeatherImage.image = UIImage(named: currentWeather.weatherType)}

代替这个

DispatchQueue.main.async{
    self.currentWeather.downloadWeatherDetails {
        self.updateMainUI()
    }
}

尝试这个

currentWeather.downloadWeatherDetails{
    DispatchQueue.main.async{
        self.updateMainUI()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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