位置坐标还不是NILL,请在加载应用程序时获得更新的标签,但如果我从另一个屏幕回来,请加载
我已经定义了一个全局变量,以在应用程序启动后立即在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论