我想将纬度数据和经度数据传递给另一个名为DestinationViewController的控制器。 DestinationViewController包含一个地图视图,因此当用户转换到新视图时,他们将根据第一个视图中的位置(纬度和经度)数据看到一个天桥地图。
现在我会解释一些问题。
第一视图
import UIKit
import MapKit
import CoreLocation
class SliderViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var slider: UISlider!
private var latitude : [Double]!
private var longtitude : [Double]!
override func viewDidLoad() {
sliderSlides(self)
}
@IBAction func sliderSlides(sender: AnyObject) {
let userChoice = Double(self.slider.value)
var realLatlong = [double]()
if userChoice == 1 {
realLatlong = [40.7484405, -73.9856644]
} else if possibility == 2 {
realLatlong = [42.7484405, -4.9856644]
} else {
realLatlong = [50.7484405, -7.9856644]
}
latitude = [realLatlong[0]]
longitude = [realLatlong[1]]
现在没有错误,完全没问题
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "sendLocationdata") {
var destination: DestinationViewController = segue.destinationViewController
as! DestinationViewController
SIGABART
destination.latitude = latitude
destination.longtitude = longitude
}
}
}
DestinationViewController
import UIKit
import MapKit
import CoreLocation
class DestinationViewController: UIViewController, MKMapViewDelegate {
var latitude : Float!
var longtitude : Float!
let distance: CLLocationDistance = 700
let pitch: CGFloat = 65
let heading = 90.0
var camera = MKMapCamera()
var coordinate: CLLocationCoordinate2D!
@IBOutlet var flyoverView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
coordinate = CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
完全没有错误
flyoverView.mapType = .SatelliteFlyover
camera = MKMapCamera(lookingAtCenterCoordinate: coordinate,
fromDistance: distance,
pitch: 0,
heading: 0)
self.flyoverView.camera = camera
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
let pitchedCamera = MKMapCamera(lookingAtCenterCoordinate: coordinate, fromDistance: distance, pitch: pitch, heading: 0)
let rotatedCamera = MKMapCamera(lookingAtCenterCoordinate: coordinate, fromDistance: distance, pitch: pitch, heading: 180)
UIView.animateWithDuration(5.0, animations: {
self.flyoverView.camera = pitchedCamera
}, completion: {
(Completed: Bool) -> Void in
UIView.animateWithDuration(25.0, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.flyoverView.camera = rotatedCamera
}, completion: nil)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
请帮帮我。如果您需要更多信息,请发表评论,谢谢!
答案 0 :(得分:1)
对于第一个错误,纬度和经度是sliderSlides函数范围内的变量,这意味着它们仅存在于该函数中。这就是您无法从prepareForSegue访问它们的原因。使它们成为类函数,使它们存在于类的范围内,方法是在任何函数之外声明它们(我建议在你的IBOutlet下)。
它应该是这样的:
@IBOutlet weak var slider: UISlider!
private var latitude:Double!
private var longitude:Double!
然后在设置它们时,只需更改现有的类变量:
,而不是使用let创建新变量latitude = realLatlong[0]
您现在应该可以在prepareForSegue中访问它们了。
另请注意:要么给它们一个初始值,如下所示:
private var latitude:Double = 0.0 //Or some default number
或检查prepareForSegue中是否为nil,因为目前只有在调用sliderSlides函数时才会设置它们。
第二个错误:
您不能使用实例变量(纬度和经度)来定义另一个实例变量(坐标)。您应该将坐标声明为类变量,但在加载视图之前不要设置其值。
替换:
let coordinate = CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
使用:
var coordinate: CLLocationCoordinate2D!
然后在你的viewDidLoad中添加:
coordinate = CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
如果你真的不想在viewDidLoad中初始化坐标,你也可以这样做:
var coordinate: CLLocationCoordinate2D {
get {
return CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
}
}
但是我强烈反对这一点,因为每次调用坐标时都会创建一个新的CLLocationCoordinate2D变量。另请注意,每当您更改纬度和经度时,这将更新坐标值(以后使用)
另请注意:纬度和经度不应为Int类型(因为您需要使用小数点)
更新:
在此处解决您的最新代码更新,因为它比评论更容易:
在SliderViewController中更改:
private var latitude : [Double]!
private var longtitude : [Double]!
到
private var latitude : Double!
private var longtitude : Double!
因为纬度和经度是单个变量,而不是数组。
在sliderSlides函数中更改:
latitude = [realLatlong[0]]
longitude = [realLatlong[1]]
到
latitude = realLatlong[0]
longitude = realLatlong[1]
因为它们是单个值,而不是数组。
然后在DestinationViewController中,更改:
var latitude : Float!
var longtitude : Float!
到
var latitude : Double!
var longtitude : Double!
因为我们需要Double值,而不是Float值
答案 1 :(得分:0)
我相信问题是,你要将经度和经度设置为Int。 尝试将数据类型更改为Float,或其他数据类型,如Double:
var latitude : Float!
var longtitude : Float!