简体   繁体   English

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

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

I'm trying to build a weather app wherein I fetch the weather data using Alamofire. 我正在尝试构建一个天气应用程序,其中我使用Alamofire获取天气数据。 The fetching of data is working fine, however the UILabel which is used to display the fetched values displays the data only for a second after which it disappears. 数据获取工作正常,但是用于显示获取的值的UILabel仅显示数据一秒钟,之后数据消失。 The labels are not part of the tableview, they sit above in UIView of the top part of the screen. 标签不是表视图的一部分,它们位于屏幕顶部的UIView中的上方。 The updateMainUI() is the method where I assign labels to the corresponding values to be displayed on screen. updateMainUI()是将标签分配给要在屏幕上显示的相应值的方法。 I have gone through solutions and they suggest using Dispatch async, however Im unable to solve this seemingly simple issue. 我已经研究了解决方案,他们建议使用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()
        }
    }
}

The updateMainUI() Method is as follows: 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)}

Instead of this 代替这个

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

try this 尝试这个

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

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

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