2016-12-09 11 views
0

マップピンから新しいvcにデータを渡すことに苦労しています。Swift:マップピンのデータを新しいVCに渡すにはどうすればよいですか?

シナリオ

マップピンデータ(名前、字幕&座標)現在JSONファイルから読み込まれます。地図ピンをドロップすると、地図ピンをクリックすると、新しいビューが開き、対応するタイトルとサブタイトル、およびその他のデータが新しいvcのラベルに表示されます。

私はthis tutorialに従ってJSONファイルからデータを抽出しています。

問題:マップピンのデータをラベルに渡す方法を見つけるのが苦労しています。

私は1つのテキストフィールドからラベルにデータを渡す前のプロジェクトを行っています。

ありがとうございました!

EDIT:

私は任意のコードを与えていないことに気付きました。

newFeatureVC

// Callout accessory 
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    let identifier = "POIAnnotations" 

    if annotation is POIAnnotations { 
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
     if annotationView == nil { 
      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 
      let button = UIButton(type: .detailDisclosure) 
      annotationView!.rightCalloutAccessoryView = button 
     } 
     else { 
      annotationView!.annotation = annotation 
     } 
     return annotationView 
    } 
    return nil 
} 

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

    if control == view.rightCalloutAccessoryView { 

     performSegue(withIdentifier: "showAnnotationInfo", sender: self) 

    } 
} 

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showAnnotationInfo" { 

     let destViewController = segue.destination as! ShopDetailViewController 
    } 
} 

POIAnnotations

このクラスは、ある名前、字幕と座標について格納された変数。

class POIAnnotations: NSObject, MKAnnotation { 


var title: String? 
var subtitle: String? 
var coordinate: CLLocationCoordinate2D 

init(title:String, subtitle:String, coordinate:CLLocationCoordinate2D) { 

    self.title = title 
    self.subtitle = subtitle 
    self.coordinate = coordinate 

    super.init() 
} 

class func fromJSON(_ json: [JSONValue]) -> POIAnnotations? { 
    // 1 
    var title: String 
    if let titleOrNil = json[1].string { 
     title = titleOrNil 
    } else { 
     title = "" 
    } 
    var subtitle: String 
    if let subtitleOrNil = json[2].string { 
     subtitle = subtitleOrNil 
    } else { 
     subtitle = "" 
    } 

    // 2 
    let latitude = (json[3].string! as NSString).doubleValue 
    let longitude = (json[4].string! as NSString).doubleValue 
    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

    // 3 
    return POIAnnotations(title: title, subtitle: subtitle, coordinate: coordinate) 
} 

}

SecondVC

class ShopDetailViewController: UIViewController { 


@IBOutlet weak var shopName: UILabel! 
@IBOutlet weak var shopRating: UILabel! 
@IBOutlet weak var cancelButton: UIBarButtonItem! 

var shopNameData = "name" 
var shopRatingData = "rating" 

私はすでにやってみました私はPOIAnnotationsクラスのJSONファイルからデータを得たのと同じ方法です。

私はfromJSON関数を呼び出して、ラベルに表示する変数に格納しようとしました。しかし、私はそれをする方法の周りに私の頭を得ることができませんでした。

+1

ここで試してみてください。データを渡して一度に1つのステップを説明するいくつかのメソッドを表示します。 – Frankie

答えて

0

MKMapViewDelegateメソッドを使用して、どのピンがタップされたかを検出し、そのデータをView Controllerに表示します。

func mapView(MKMapView, didSelect: MKAnnotationView) 
関連する問題