如何从视图控制器传递数据? [英] How to Pass Data from View Controllers?

查看:92
本文介绍了如何从视图控制器传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass a latitude data and longitude data to another controller called DestinationViewController. DestinationViewController contains a map view, so as user transitions to a new view, they will see a flyover map based on the location(latitude and longitude) data in the first view.

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

Now there is a few problems which I will explain along the way.

第一视图

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

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

}

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

Please help me. Leave a comment if you need more information, thanks!

推荐答案

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

For your first error, latitude and longitude are variables in the scope of the sliderSlides function, meaning they exist only within that function. This is why you can't access them from your prepareForSegue. Make them class functions so that they exist in the scope of the class by declaring them outside of any function (I would suggest under your IBOutlet).

应该是这样的:

@IBOutlet weak var slider: UISlider!

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

然后,当您设置它们时,只需更改现有的类变量即可,而不用用let创建一个新变量:

Then when you set them, instead of creating a new variable with let, just change the existing class variable:

latitude = realLatlong[0]

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

You should be able to access them now in your prepareForSegue.

还请注意:可以给它们一个初始值,如下所示:

Note also: Either give them an initial value like so:

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

或检查prepareForSegue中是否为nil,因为当前只有在调用slideSlides函数时才会设置它们.

or check whether it is nil in prepareForSegue, because currently they will only get set if the sliderSlides function gets called.

第二个错误:

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

You can't use your instance variables(latitude and longitude) to define another instance variable (coordinate). You should declare coordinate as a class variable, but not set its value until the view is loaded.

替换:

let coordinate = CLLocationCoordinate2D(latitude: latitude,
                                        longitude: longitude)

具有:

var coordinate: CLLocationCoordinate2D!

,然后在您的viewDidLoad中添加:

and then in your viewDidLoad add:

coordinate = CLLocationCoordinate2D(latitude: latitude,
                                    longitude: longitude)

如果您真的不想在viewDidLoad中初始化坐标,也可以执行以下操作:

If you really don't want to initialise the coordinate in the viewDidLoad you can also do:

var coordinate: CLLocationCoordinate2D {
    get {
        return CLLocationCoordinate2D(latitude: latitude,
                                    longitude: longitude)
    }
}

但是我强烈建议不要这样做,因为每次调用坐标时,您都会创建一个新的CLLocationCoordinate2D变量.另请注意,每当您更改经度和纬度时,这都会更新坐标值(以备将来使用)

However I would highly suggest against this as you will be creating a new CLLocationCoordinate2D variable every time you call coordinate. Note also that this will update the coordinate value (in future use) whenever you change latitude and longitude

还请注意:纬度和经度不应为Int类型(因为您需要使用小数点)

Also note: latitude and longitude shouldn't be of type Int (as you are requiring the use of the decimal points)

更新:

在此处解决您的最新代码更新,因为它比注释中的更新容易:

Addressing your latest code update here because it is easier than in the comments:

在SliderViewController中进行更改:

In your SliderViewController change:

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

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

因为纬度和经度是单个变量,而不是数组.

Because latitude and longitude are single variables, not arrays.

在sliderSlides功能更改中:

In the sliderSlides function change:

latitude = [realLatlong[0]]
longitude = [realLatlong[1]]

latitude = realLatlong[0]
longitude = realLatlong[1]

再次,它们是单个值,而不是数组.

Because again, they are single values, not arrays.

然后在DestinationViewController中,更改:

Then in your DestinationViewController, change:

var latitude : Float!
var longtitude : Float!

var latitude : Double!
var longtitude : Double!

因为我们需要Double值,而不是Float值

Because we need Double values, not Float values

这篇关于如何从视图控制器传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆