位置坐标还不是NILL,请在加载应用程序时获得更新的标签,但如果我从另一个屏幕回来,请加载

发布于 2025-02-08 22:29:39 字数 2175 浏览 2 评论 0原文

我已经定义了一个全局变量,以在应用程序启动后立即在AppDelegate中存储位置坐标和IM更新位置。这是为此的代码:

var locationManager:CLLocationManager!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // Override point for customization after application launch.
        
      window = UIWindow(frame: UIScreen.main.bounds)
        
      determineCurrentLocation()
        
      self.pushToHomeScreen()
        
      return true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if GlobalUserManager.shared.currentLocation == nil {
            GlobalUserManager.shared.currentLocation = locations.last
            locationManager.stopUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error - locationManager: \(error.localizedDescription)")
    }

    //MARK:- Intance Methods
    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }

之后,主屏幕打开了我有两个标签以显示位置数据的地方。当我尝试通过将代码放入viewDidload()viewWillAppear()中,当我尝试将代码放入viewDidload()时,它都没有得到更新。因此,我尝试将代码放入viewDidappear()及其类似的内容中:

override func viewDidAppear(_ animated: Bool) {
        if GlobalUserManager.shared.currentLocation != nil {
            lblLat.text = GlobalUserManager.shared.currentLocation.latitude
            lblLong.text = GlobalUserManager.shared.currentLocation.longitude
        }
        else {
            lblLat.text = "Lat not available"
            lblLong.text = "Long not available"
        }
    }

当屏幕加载时,标签为空,至少意味着globalusersermanager.shared.currentlocation.currentlocation!= nil nil 但是我希望他们在加载屏幕后立即进行更新。取而代之的是,我必须单击另一个页面然后返回,然后进行更新。

I have defined a global variable to store location coordinates and im updating location in Appdelegate as soon as the app launches. Here's the code for that:

var locationManager:CLLocationManager!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // Override point for customization after application launch.
        
      window = UIWindow(frame: UIScreen.main.bounds)
        
      determineCurrentLocation()
        
      self.pushToHomeScreen()
        
      return true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if GlobalUserManager.shared.currentLocation == nil {
            GlobalUserManager.shared.currentLocation = locations.last
            locationManager.stopUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error - locationManager: \(error.localizedDescription)")
    }

    //MARK:- Intance Methods
    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }

After this the Home Screen opens where I have two labels to show location data. It didn't get update when Homescreen is loaded for both cases when I tried to update by putting code in viewdidLoad() and viewWillAppear(). So I tried putting code in viewDidAppear() and its something like this:

override func viewDidAppear(_ animated: Bool) {
        if GlobalUserManager.shared.currentLocation != nil {
            lblLat.text = GlobalUserManager.shared.currentLocation.latitude
            lblLong.text = GlobalUserManager.shared.currentLocation.longitude
        }
        else {
            lblLat.text = "Lat not available"
            lblLong.text = "Long not available"
        }
    }

When screen is loaded, labels are empty which means at least GlobalUserManager.shared.currentLocation != nil but I want them to get updated as soon as screen is loaded. Instead I have to click another page and come back and then they are updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文