在我的应用委托(delegate)类中,我声明了 locaitonManager:
var locationManager: CLLocationManager?
稍后在我的应用程序中的一个 View Controller 类中,我需要停止位置更新:
locationManager?.stopUpdatingLocation()
但是我得到错误:
Value of type CLLocation Manager.... has no member ....
因为 View Controller 无法访问在应用程序委托(delegate)类中声明的位置管理器。我的简单出路是什么?我必须在应用程序委托(delegate)中拥有 locationManager 访问权限。
注意:奇怪的是,我的
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
也在 View Controller 内,它似乎工作正常。
最佳答案
回答我自己的问题以备将来引用:
在 ViewController 的类中:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
然后在代码的后面,我可以像这样引用 App Delegate 中声明的 locationManager:
appDelegate.locationManager!.requestAlwaysAuthorization()
希望这对其他人有帮助。
关于ios - 从 AppDelegate 和 ViewController 访问 locaitonManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33576366/