2016-06-15 8 views
0

名前、説明、および座標オブジェクトを持つRealmオブジェクトPlaceがあります。 mapViewがロードされると、Realmオブジェクトのインスタンスごとにピンが作成されます。私が達成したいのは、各ピンのアノテーションをクリックすると、その場所に関する詳細情報を提供する詳細ビューに来るということです。このPlaceオブジェクトをカスタムアノテーションに渡して、prepareForSegue関数の属性を使用してアクセスし、DetailViewControllerで操作できるようにする方法はありますか?オブジェクトをカスタム注釈ビューに渡す

は、ここに私のCustomAnnotationクラスです:ここViewControllerの機能と

import Foundation 
import UIKit 
import MapKit 
import RealmSwift 

class CustomAnnotation: MKPointAnnotation { 
    var place = Place() 
} 

そしてmapView

func loadLocations() { 
     for place in realm.objects(Place) { 
      let userLocationCoordinates = CLLocationCoordinate2DMake(place.latitude, place.longitude) 
      let pinForUserLocation = CustomAnnotation() 
      pinForUserLocation.coordinate = userLocationCoordinates 
      pinForUserLocation.title = place.name 
      pinForUserLocation.subtitle = place.placeDescription 
      pinForUserLocation.place = place 
      mapView.addAnnotation(pinForUserLocation) 
      mapView.showAnnotations([pinForUserLocation], animated: true) 
     } 
    } 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     if !(annotation is CustomAnnotation) { 
      return nil 
     } 
     let reuseId = "customAnnotation" 
     var view = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if view == nil { 
      view = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      view!.image = UIImage(named:"locationAnnotation") 
      view!.leftCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) 
      view!.canShowCallout = true 
     } 
     else { 
      view!.annotation = annotation 
     } 
     return view 
    } 

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     performSegueWithIdentifier("showPlaceDetailSegue", sender: annotation) 
    } 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "showPlaceDetailSegue" { 
      let vc = segue.destinationViewController as! PlaceDetailViewController 
      vc.name = sender!.title 
      vc.descriptionText = sender!.subtitle 
      vc.coordinate = sender!.coordinate 
      vc.place = sender!.place 
     } 
    } 

答えて

1

view.annotationによって、注釈へのアクセスと

にカスタムクラスにキャスト
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
let annotation = view.annotation as! CustomAnnotation 
    performSegueWithIdentifier("showPlaceDetailSegue", sender: annotation) 
} 
+0

それは私に "引数渡し'view = CustomAnnotationView(注釈:注釈、reuseIdentifier:reuseId)'に変更すると、引数を取らないように呼び出すことができます。私はまだ私のCustomAnnotationクラスにいくつかのエラーがあると思いますか? – Luk579

+0

申し訳ありません...編集を参照してくださいしてみてください... – tbilopavlovic

+0

まさに私が必要なもの!ありがとう! – Luk579

関連する問題