0

my JSON have been printed to console with the correct value request. but label.text and city.text won't update UI in main controller

@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
}

}

I am new to coding and 'm having a hard figuring out why isn't it showing since I confirmed that I am pulling data from Json to my console

1 Answers1

0

These lines won't do what you expect.

locationManager.requestWhenInUseAuthorization()
locationAuthStatus()

Your call to requestWhenInUseAuthorization is asynchronous and should be handled by a CLLocationManagerDelegate which can be your view controller. You can do this like this:

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

Add these functions to your 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()

}
Stephen O'Connor
  • 1,465
  • 1
  • 10
  • 20