簡體   English   中英

將數據從 AppDelegate 傳遞到 ViewController

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM