gpt4 book ai didi

ios - 从 Openweathermap IOS 4 Xcode 10 获取 json 数据后 UI 不会更新

转载 作者:行者123 更新时间:2023-11-30 11:16:33 25 4
gpt4 key购买 nike

我的 JSON 已使用正确的值请求打印到控制台。但 label.text 和 city.text 不会更新主 Controller 中的 UI

@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var weatherType: UILabel!
@IBOutlet weak var degree: UILabel!
@IBOutlet weak var city: UILabel!

var currentWeather : CurrentWeather!

override func viewDidLoad() {
super.viewDidLoad()
currentWeather = CurrentWeather()

}
override func viewDidAppear(_ animated: Bool) {
super .viewDidAppear(animated)
locationAuthStatus()

}

func locationAuthStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentLocaion = locationManager.location
//Location.sharedIntance.latitude = currentLocaion.coordinate.latitude
//Location.sharedIntance.longitude = currentLocaion.coordinate.longitude
print(currentLocaion)
currentWeather.downloadWeatherDetail {
downloadForecastData {
self.updateMainUI()
}
}

} else {
locationManager.requestWhenInUseAuthorization()
locationAuthStatus()
}
}


func updateMainUI() {
icon.image = UIImage(named: currentWeather.weatherType)
city.text = currentWeather.cityName
degree.text = "\(currentWeather.currentTemp)"
weatherType.text = currentWeather.weatherType
date.text = currentWeather.date
}

}

我是编码新手,自从我确认将数据从 Json 提取到控制台以来,我很难弄清楚为什么它没有显示

最佳答案

这些行不会达到您的预期。

locationManager.requestWhenInUseAuthorization()
locationAuthStatus()

您对 requestWhenInUseAuthorization 的调用是异步的,应该由 CLLocationManagerDelegate 处理,它可以是您的 View Controller 。你可以这样做:

class ViewController: UITableViewController, CLLocationManagerDelegate {
// rest of class

将这些函数添加到您的类中

func locationManager(_ manager: CLLocationManager, didUpdateLocations objects: [CLLocation]) {
let location = objects[objects.count-1]
let currentLocation = location
print(currentLocation)
currentWeather.downloadWeatherDetail {
downloadForecastData {
self.updateMainUI()
}
}
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// handle error
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
locationManager.startUpdatingLocation()

}

关于ios - 从 Openweathermap IOS 4 Xcode 10 获取 json 数据后 UI 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51691330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com