2017-02-06 16 views
0

注釈を実行するためにSegueを実行しようとしていますが、このコードは異なるインデックスを生成し、間違った場所がdetailsViewに表示されます。どのようにこれを修正することが可能かを知ることは素晴らしいことでしょう。注釈のインデックスを見つけるMapKit

おかげ

var detailsDict = [NSDictionary]() 
var selectedPlace: NSDictionary! 

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 

    if (view.annotation?.isKind(of: PlaceAnnotation.self))! { 
    let annotation = view.annotation 
    let index = (self.mapView.annotations as NSArray).index(of: annotation!) 
    let place = detailsDict[index] 
    selectedPlace = place 

    performSegue(withIdentifier: "showMuseumDetails", sender: self) 
    } 
} 

答えて

0

私はここでの主な問題は、あなたがmapView.annotationsは、すべての注釈があなたのdetailsDict -arrayと同じ順序であることで配列を返すことを期待していることだと思います。この仮定はエラーが起こりやすいと思います。なぜなら、MapKitフレームワークの内部実装に大きく依存するからです。

あなたがマップに追加PlaceAnnotationと一緒に辞書エントリを保存することになる良い方法:

  • あなたがPlaceAnnotation
  • を作成するときに details
  • PlaceAnnotationクラスに設定 details値新しいプロパティを追加します。
  • calloutAccessoryControlTappedに設定し、PlaceAnnotationの詳細プロパティを取得します
  • この値を詳細コントローラに転送します(selectedPlaceプロパティですが、私は直接ViewControllerをinstatiatingして値を設定することをお勧めします)。
1

私たちは、この方法

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
let annotation = view.annotation 
let index = (self.mapView.annotations as NSArray).index(of: annotation!) 
print ("Annotation Index = \(index)") 

} 
を使用してそれを行うことができます
関連する問題