如何从视图控制器传递数据?

我想将纬度数据和经度数据传递给另一个名为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. } */ } 

请帮帮我。 如果您需要更多信息,请留下评论,谢谢!

对于第一个错误,纬度和经度是sliderSlides函数范围内的variables,这意味着它们只存在于该函数内。 这就是为什么你不能从你的prepareForSegue访问它们。 使他们类function,以便它们存在于类的范围内,通过在任何函数之外声明它们(我build议在您的IBOutlet下)。

它应该是这样的:

 @IBOutlet weak var slider: UISlider! private var latitude:Double! private var longitude:Double! 

然后当你设置它们,而不是用let创build一个新的variables,只需要改变现有的类variables:

 latitude = realLatlong[0] 

你现在可以在prepareForSegue中访问它们了。

还要注意:或者给它们一个初始值,如下所示:

 private var latitude:Double = 0.0 //Or some default number 

或者在prepareForSegue中检查它是否为零,因为当前只有在调用了sliderSlides函数时才会设置它们。

对于你的第二个错误:

您不能使用您的实例variables(经度和纬度)来定义另一个实例variables(坐标)。 您应该将坐标声明为类variables,但在视图加载之前不要设置其值。

更换:

 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) } } 

不过,我会强烈build议不要这样做,因为每次调用坐标时都会创build一个新的CLLocationCoordinate2Dvariables。 还要注意,无论何时改变纬度和经度,这将会更新坐标值(将来使用)

另请注意:纬度和经度不应该是Inttypes(因为您要求使用小数点)

更新:

在这里解决你最新的代码更新,因为它比评论中的更容易:

在你的SliderViewController中更改:

 private var latitude : [Double]! private var longtitude : [Double]! 

 private var latitude : Double! private var longtitude : Double! 

因为经度和纬度是单个variables,而不是数组。

在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值

我相信问题是,你正在设定Int的经纬度。 尝试将数据types更改为Float或其他数据types,如Double:

 var latitude : Float! var longtitude : Float!