簡體   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