视图加载之前,位置不更新

我有一个variables“zipCode”,我试图更新到用户当前位置的视图加载在我的scrollView。 目前,当我退出并回到scrollView视图控制器时,它会更新到正确的位置。 此外,我应该实施全新安装授权更新的更改。 感谢您的帮助。

import UIKit import CoreLocation var zipCode = 90210 var location = [""] class ViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() @IBOutlet var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() } ////Current Location func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error)-> Void in if (error != nil) { print("Error: + error.localizedDescription") return } if placemarks!.count > 0 { let pm = placemarks![0] self.displayLocationInfo(pm) }else { print("Error with data") } }) } func displayLocationInfo(placemark: CLPlacemark) { self.locationManager.stopUpdatingLocation() let zip = placemark.postalCode if let zip2 = numberFormatter.numberFromString(zip!)?.integerValue { zipCode = zip2 } } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("Error: " + error.localizedDescription) } ///// Load Views override func viewWillAppear(animated: Bool) { let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard0") as UIViewController! self.addChildViewController(vc0) self.scrollView.addSubview(vc0.view) vc0.didMoveToParentViewController(self) vc0.view.frame = scrollView.bounds vc0.viewDidLoad() let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard2") as UIViewController! self.addChildViewController(vc1) self.scrollView.addSubview(vc1.view) vc1.didMoveToParentViewController(self) vc1.view.frame = scrollView.bounds var vc1Frame: CGRect = vc1.view.frame vc1Frame.origin.x = self.view.frame.width vc1.view.frame = vc1Frame vc1.viewDidLoad() let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard1") as UIViewController! self.addChildViewController(vc2) self.scrollView.addSubview(vc2.view) vc2.didMoveToParentViewController(self) vc2.view.frame = scrollView.bounds var vc2Frame: CGRect = vc2.view.frame vc2Frame.origin.x = 2 * self.view.frame.width vc2.view.frame = vc2Frame vc2.viewDidLoad() let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard3") as UIViewController! self.addChildViewController(vc3) self.scrollView.addSubview(vc3.view) vc3.didMoveToParentViewController(self) vc3.view.frame = scrollView.bounds var vc3Frame: CGRect = vc3.view.frame vc3Frame.origin.x = 3 * self.view.frame.width vc3.view.frame = vc3Frame vc3.viewDidLoad() self.scrollView.contentSize = CGSizeMake((self.view.frame.width) * 4, (self.view.frame.height)) } } 

你注意到的是由于CLLocationManager是一个asynchronous类,这意味着从你开始的那一刻到你接收到这个位置之间可能会有一个延迟。 这个延迟可能并不总是很大,但在大多数情况下足够让didUpdateLocationsviewDidLoad之后运行。

同样,如果用户尚未授权您的应用程序的位置使用情况,您只能在用户按下允许(或者如果用户不批准您会收到失败)后才能获取位置。

几点build议:

  1. 准备在视图显示后接收位置,例如显示一些信息文本(或微调)。 或者要求尽快find位置,如果您还没有收到位置(不知何故告知用户),请不要前往有问题的控制器,
  2. 根据您的应用程序的规格,您可以仅在需要时(如在您的代码中)或在启动时请求位置授权
  3. 将与位置pipe理器相关的代码封装到您的自定义类中,您可以简单地询问它的位置,当您需要在多个控制器中使用位置时,这会使事情更容易。

在nshipster.com上有一篇关于CoreLocation的非常有用的文章,我build议你通过它,它将阐明iOS中位置服务的许多方面。 你可以在这里find这篇文章