2017-07-06 30 views
0

我有兩個視圖控制器 - 一個是可以通過locationManager獲取用戶位置座標的mapView,另一個是我希望能夠拉動這些用戶座標的VC。Swift 3.0從不同的視圖控制器獲取用戶位置座標

第一個VC:MapView的

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    var coordinatesOfUser = locations.last?.coordinate 
    print("The value of usercoordinates are \(coordinatesOfUser)") 

    // here I want to be able to pull this variable, coordinatesOfUser 


    if let location = locations.last { 
     let span = MKCoordinateSpanMake(0.00775, 0.00775) 
     let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude) 
     let region = MKCoordinateRegionMake(myLocation, span) 
     map.setRegion(region, animated: true) 
    } 
    self.map.showsUserLocation = true 
    locationManager.stopUpdatingLocation() 


} 

二VC:

我想調用這個VC的功能的LocationManager的。這是將座標拉到VC的最有效方法嗎?如果是這樣,我該如何去做呢?

+0

它可以很容易地用MVVM模式實現http://rasic.info/bindings-generics-swift-and-mvvm/。兩個視圖控制器都使用一個視圖模型。 –

回答

2

這裏有幾個選項來解決這個問題:

代表團:您secondVC可以有一個委託,允許第一個視圖控制器從它那裏得到的座標。這裏的好處是,你可以收到更新的進來

protocol MyLocationDelegate: class { 
    func newLocationArrived(location: CLLocation) 
} 

class FirstVC: UIViewController, MyLocationDelegate { 

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

    func newLocationArrived(location: CLLocation) { 
     print(location) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if let dest = segue.destination as? SecondVC { 
      dest.delegate = self 
     } 
    } 
} 

class SecondVC: UIViewController, CLLocationManagerDelegate { 

    /// ... ... 

    weak var delegate: MyLocationDelegate? 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     /// do something with the location 

     /// provide the data via delegation 
     delegate?.newLocationArrived(location: CLLocation()) 
    } 
} 

聲明:郵政通過NSNotificationCenter的通知。也可以收到更新,只需通過通知中心而不是通過代理髮送。

postNotificationName:對象:用戶信息:

兒童視圖控制器:根據第二視圖控制器和視圖是否是第一個孩子,這可能允許直接訪問。並不總是一種選擇。

單身人士(CLLocationManager):如果您打算在整個應用程序的其他地方使用位置服務,您可以使用Singleton將CLLocationManager移動到它自己的類中。其他視圖控制器可以引用該類以滿足其特定需求。這在使用後臺或重要更改位置時也很有用,因爲它們可能需要使用LaunchOptions項來重新啓動位置管理器。

class MyLocationManager: CLLocationManager, CLLocationManagerDelegate { 

    static let shared = MyLocationManager() 

    var locations = [CLLocation]() 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     for location in locations { 
      self.locations.append(location) 
     } 
    } 
} 
0

我有同樣的問題,我終於用通知修正了它,正如kuhncj所說。

這是怎樣的代碼看起來像在最後:

//Get user location 
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 

      if status == .authorizedWhenInUse { 
       locationManager.startUpdatingLocation() 

       mapView.isMyLocationEnabled = true 
       mapView.settings.myLocationButton = true 
      } else { 
       initialLocation() 
      } 
     } 

     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

      if let location = locations.first { 

       mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) 

       locationManager.stopUpdatingLocation() 

       let userInfo: NSDictionary = ["location": location] 

       //Post notification 
       NotificationCenter.default.post(name: NSNotification.Name("UserLocationNotification"), object: self, userInfo: userInfo as [NSObject : AnyObject]) 

      } 
     } 

然後在其他視圖控制器:

var userLocation: String? 

    override func viewDidLoad() { 
      super.viewDidLoad() 

      //Observer that receives the notification 
      NotificationCenter.default.addObserver(self, selector: #selector(locationUpdateNotification), name: Notification.Name("UserLocationNotification"), object: nil) 

     } 

     func locationUpdateNotification(notification: NSNotification) { 
      if let userInfo = notification.userInfo?["location"] as? CLLocation { 
       //Store user location 
       self.userLocation = "\(userInfo.coordinate.latitude), \(userInfo.coordinate.longitude)" 
      } 
     } 

然後我就能夠使用存儲用戶位置的另一方法。

相關問題