destinationViewController

destinationViewController

我想将纬度数据和经度数据传递给另一个名为DestinationViewController的控制器。 DestinationViewController包含一个 map 视图,因此当用户过渡到新视图时,他们将在第一个视图中基于位置(纬度和经度)数据看到一个跨接 map 。

现在有一些问题,我将一路解释。

第一画面

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函数范围内的变量,这意味着它们仅存在于该函数中。这就是为什么您无法从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值

10-07 21:53