[英]Pass data from AppDelegate to 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)
}
}
如果您真的想這樣做,您可以在每次MapViewController
加載視圖時從AppDelegate
獲取您的位置,如下所示:
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 它不會在那里,你需要將任何邏輯移到一個方法中,並在你確定它會去的地方調用它在那里。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.