将数据从 AppDelegate 传递到 ViewController



我知道这个问题已经被问了很多,但我找不到与我的情况类似的问题。我使用Google Maps SDK,我想将我当前的位置坐标从应用程序代表传递到我的ViewController。我知道如何传递它let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate但我想在每次加载视图时更改我的位置。所以我只需要我当前的位置在开始时。

应用代表:

import UIKit
import GoogleMaps
import GooglePlaces
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var currentLocation:CLLocation?
var locationManager:CLLocationManager?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GMSServices.provideAPIKey("KEY")
GMSPlacesClient.provideAPIKey("KEY")
setupLocationManager()
return true
}
func setupLocationManager(){
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager?.startUpdatingLocation()
}
// Below method will provide you current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if currentLocation == nil {
currentLocation = locations.last
locationManager?.stopMonitoringSignificantLocationChanges()
let locationValue:CLLocationCoordinate2D = manager.location!.coordinate
print(locationValue)//THIS IS WHAT I WANT TO PASS
locationManager?.stopUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error")
}

}

视图控制器:

import UIKit
import GoogleMaps
class MapViewController: UIViewController,  GMSMapViewDelegate  {
@IBOutlet weak var mapView: GMSMapView!
var location : CLLocationCoordinate2D?

override func viewDidLoad() {
super.viewDidLoad()
print(location)
}

}

如果你真的想这样做,你可以从AppDelegate每次视图加载到你的MapViewController时获取你的位置,如下所示:

import UIKit
import GoogleMaps
class MapViewController: UIViewController, GMSMapViewDelegate  {
@IBOutlet weak var mapView: GMSMapView!
var location : CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
self.location = appDelegate.currentLocation
print(location)
}
}

不过,我认为AppDelegate确实不是拥有这种CLLocationManager逻辑的正确地方。 如果这是使用该位置的唯一UIViewController,则可以考虑在MapViewController中实现该逻辑。 如果它由许多屏幕共享,也许您应该创建一个作为单一实例实现的LocationManager类,以便所有UIViewController子类都可以访问最新的位置数据。

您可以在 MapViewController 上扩展 CLLocationManagerDelegate,这样您就可以从视图控制器访问任何变量

创建新文件:MapViewCLLocationExtension.swift

import Foundation
import UIKit
import GoogleMaps
import GooglePlaces
extension MapViewController: CLLocationManagerDelegate {
...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if currentLocation == nil {
currentLocation = locations.last
locationManager?.stopMonitoringSignificantLocationChanges()
let locationValue:CLLocationCoordinate2D = manager.location!.coordinate
//print(locationValue)//THIS IS WHAT I WANT TO PASS
location = locationValue
locationManager?.stopUpdatingLocation()
//You should call a method here for any further thing you need to 
//do with location. DO NOT USE location on viewDidLoad
useLocation()
}
}

我必须警告你,你不能在viewDidLoad((上打印位置,b/c它还没有,你需要将任何逻辑移动到一个方法并在你确定它会在那里的地方调用它。

希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新