私は2つのView Controllerを持っています.1つはlocationManagerを介してユーザーの位置座標を取得できるmapView、もう1つはこれらのユーザー座標を取得できるVCです。Swift 3.0別のView Controllerからユーザーの位置座標を取得
まず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に座標を引き込む最も効率的な方法ですか?もしそうなら、私はそれをどうやってやりますか?
委任::あなたのsecondVCはそれから座標を取得するための最初のビューコントローラを可能にデリゲートを持つことができます。ここ
MVVMパターンhttp://rasic.info/bindings-generics-swift-and-mvvm/で簡単に実現できます。両方のビューコントローラに1つのビューモデルを使用します。 –