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

How to Pass Data from View Controllers?

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

NO error now, completely fine

}

  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)

No error at all

        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.
    }
    */

}

请帮助我。如果您需要更多信息,请发表评论,谢谢!

我认为问题在于,您将纬度和经度设置为 Int。 尝试将数据类型更改为 Float,或其他类似 Double 的数据类型:

var latitude : Float!
var longtitude : Float!

对于您的第一个错误,纬度和经度是 sliderSlides 函数范围内的变量,这意味着它们仅存在于该函数中。这就是为什么您无法从 prepareForSegue 访问它们的原因。通过在任何函数之外声明它们,使它们成为 class 函数,以便它们存在于 class 的范围内(我建议在您的 IBOutlet 下)。

应该是这样的:

@IBOutlet weak var slider: UISlider!

private var latitude:Double!
private var longitude:Double!

然后当你设置它们时,不用用 let 创建一个新变量,只需更改现有的 class 变量:

latitude = realLatlong[0]

您现在应该可以在 prepareForSegue 中访问它们。

另请注意:要么像这样给它们一个初始值:

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

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

对于你的第二个错误:

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

替换:

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 值