简体   繁体   中英

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. 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. The labels are not part of the tableview, they sit above in UIView of the top part of the screen. The updateMainUI() is the method where I assign labels to the corresponding values to be displayed on screen. I have gone through solutions and they suggest using Dispatch async, however Im unable to solve this seemingly simple issue.

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:

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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