2016-06-15 11 views
0

Ich habe ein Realm-Objekt Place mit Namen, Beschreibung und Koordinatenobjekten. Wenn die mapView geladen wird, wird für jede Instanz des Realm-Objekts eine PIN erstellt. Was ich erreichen möchte, ist, dass Sie, wenn Sie auf die Annotation jedes Pins klicken, zu einer Detailansicht kommen, die Ihnen mehr Informationen über den Ort geben wird. Gibt es eine Möglichkeit, dieses Place-Objekt an die benutzerdefinierte Annotation zu übergeben, damit ich seine Attribute in der prepareForSegue-Funktion verwenden und auf die DetailViewController zugreifen und sie bearbeiten kann?Objekt an benutzerdefinierte Annotationsansicht übergeben

Hier ist meine CustomAnnotation Klasse:

import Foundation 
import UIKit 
import MapKit 
import RealmSwift 

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

Und hier die Funktionen im ViewController mit dem 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 
     } 
    } 

Antwort

1

Zugang zu Annotation von view.annotation und wirft es auf Ihre eigene Klasse in

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
let annotation = view.annotation as! CustomAnnotation 
    performSegueWithIdentifier("showPlaceDetailSegue", sender: annotation) 
} 
+0

Es sagt mir "Argument pass Ed zum Aufruf, das keine Argumente annimmt "wenn zu" view = CustomAnnotationView (annotation: annotation, reuseIdentifier: reuseId) "gewechselt wird. Ich glaube, ich habe immer noch Fehler in meiner CustomAnnotation-Klasse? – Luk579

+0

Sorry ... bitte siehe bearbeiten und versuchen Sie das ... – tbilopavlovic

+0

Genial genau das, was ich brauchte! Vielen Dank! – Luk579

Verwandte Themen