【发布时间】:2016-04-15 17:28:26
【问题描述】:
我有一个使用 CLLocationManager 获取用户当前纬度/经度的按钮,我试图将纬度/经度都传递到新的视图控制器中。获取纬度/经度有效,但是当我尝试将数据传递到我的新视图控制器时,它没有被传递。
func getCurrentLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let currentLocation: CLLocation = locations[0]
self.userLongtitude = currentLocation.coordinate.longitude
self.userLatitude = currentLocation.coordinate.latitude
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! ListOfEventsTableViewController
if segue.identifier == "useCurrentLocation" {
getCurrentLocation()
destinationVC.userCurrentLat = self.userLatitude
destinationVC.userCurrentLong = self.userLongtitude
}
}
有什么帮助吗?
编辑:
func getCurrentLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
self.userLongtitude = location.coordinate.longitude
self.userLatitude = location.coordinate.latitude
self.performSegueWithIdentifier("useCurrentLocation", sender: nil)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! ListOfEventsTableViewController
if segue.identifier == "useCurrentLocation" {
destinationVC.userCurrentLat = self.userLatitude
destinationVC.userCurrentLong = self.userLongtitude
}
}
【问题讨论】:
标签: swift cllocationmanager uistoryboardsegue