jeudi 23 juin 2016

How to Pass Data from View Controllers?

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. FIRST VIEW 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. } */ } Please help me. Leave a comment if you need more information, thanks!

Aucun commentaire:

Enregistrer un commentaire