2016-06-23 12 views
1

緯度データと経度データをDestinationViewControllerという別のコントローラに渡したいとします。 DestinationViewControllerにはマップビューが含まれているため、ユーザーが新しいビューに移行すると、最初のビューの位置(緯度と経度)データに基づいてフライオーバーマップが表示されます。View Controllerからデータを渡す方法は?

ここで私が途中で説明するいくつかの問題があります。

最初のビュー

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

} 

私を助けてください。もっと情報が必要な場合はコメントを残してください、ありがとう!

答えて

1

: フロートするデータの種類を変更してみてください、またはダブルのような何か他のデータ型。これは、あなたが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 

またはsliderSlides関数が呼び出された場合、現在、彼らは唯一のセットを取得しますので、それは、prepareForSegueにnilであるかどうかを確認します。彼らにそのような初期値を与えるどちらか。 2番目のエラーの

別のインスタンス変数を定義するインスタンス変数(緯度と経度)を使用することができない(座標)。座標をクラス変数として宣言する必要がありますが、ビューがロードされるまでその値は設定しないでください。

は交換してください:

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

て:あなたのviewDidLoadで

var coordinate: CLLocationCoordinate2D! 

、その後、追加します。

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]! 

UPDATE:それはコメントよりも簡単なので

ここに最新のコード更新アドレッシング

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

から

緯度と経度は、単一の変数ではなく配列されているからです。 sliderSlides機能変化

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

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

再度、それらは単一の値ではなく配列されているからです。次に、あなたのDestinationViewController、変化

:我々はDouble値を必要とするので

var latitude : Float! 
var longtitude : Float! 

​​

に、値

私はあなたが提案したものでした
+0

フロートではない、私は、2つのエラーを得ました私の新しい編集を見てください。 –

+0

経度と緯度を浮動小数点から倍精度に変更します。次に、sliderSlidesで経度と緯度を設定するときに、単一の値ではなく配列にします。私は私の答えを更新しました – Sam

+0

とにかく@IBAction funcの外でスライダーの値を取得しますか? –

0

私は問題があると信じています。あなたはIntに緯度と経度を設定しています。 、緯度と経度は、彼らがその関数内でのみ存在する意味、sliderSlides機能のスコープ内の変数であるあなたの最初のエラーのために

var latitude : Float! 
var longtitude : Float! 
関連する問題