iOS CLLocationManager在一个单独的类中 [英] iOS CLLocationManager in a separate class

查看:187
本文介绍了iOS CLLocationManager在一个单独的类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个视图控制器需要获取用户的位置,所以我想创建一个单独的类,ViewControllers可以调用它来获取用户的最新位置。

I have multiple view controllers that need to get the user's location, so I wanted to create a separate class that the ViewControllers can call to get the user's latest location.

locationManager:didUpdateToLocation:fromLocation返回void。一旦计算出用户的纬度和经度,我如何将纬度和经度数据传回我的ViewControllers?

locationManager:didUpdateToLocation:fromLocation returns void. How do I pass the latitude and longitude data back to my ViewControllers as soon as the user's latitude and longitude is calculated?

我可以尝试在我的locationManaging类中编写getter和setter ,但如果我这样做,我怎么知道何时从我的ViewController类调用纬度getter和经度getter方法?如何保持ViewController的主线程等待来自locationManaging类的纬度和经度值?

I could try writing getters and setters in my locationManaging class, but if I do that, how do I know when to call the latitude getter and longitude getter methods from my ViewController class? How do I hold the ViewController's main thread to wait for the latitude and longitude values from the locationManaging class?

谢谢!

推荐答案

创建一个具有纬度经度属性的单例类, startLocating endLocating 。在类中,创建一个 CLLocationManager 实例,并将其委托设置为单例。在 startLocating endLocating 中,调用 CLLocationManager的适当方法实例。使委托方法更新纬度经度属性。在其他 ViewControllers 中,读取此单例的纬度经度属性。

Create a singleton class which has a latitude and longitude properties, startLocating and endLocating. In the class, create a CLLocationManager instance, and set its delegate to be the singleton. In startLocating and endLocating, call the appropriate methods of the CLLocationManager instance. Make the delegate methods update the latitude and longitude properties. In other ViewControllers, read this singleton's latitude and longitude properties.

要知道何时从另一个 ViewController 读取这些属性,请在这些属性上设置一个观察者(参见 NSKeyValueObserving Protocol Reference

To know when to read those properties from another ViewController, set an observer on these properties (see the NSKeyValueObserving Protocol Reference

在执行此操作之前,请查找Internets以获取现有代码。

Before doing this, look up the Internets for existing code.

执行此操作后,将其上传到GitHub,许可证。

After doing this, upload it to GitHub with a permissive license.

这篇关于iOS CLLocationManager在一个单独的类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆