Koppi
Koppi

Reputation: 37

set the callout annotation title from "DidSelectAnnotationView" in Swift 2.0 and Data From CoreData

please help. =) i have a problem to set the callout title. I receive the informations from core data with predicates. When i click on a annotation on the map, i receive all data from the pin in my Logs.

how can set the title in the callout Bubble?

  func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

  let annotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "CheckpointPins")

 if annotation is MKUserLocation {
     return nil }
  else {

        let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
        let items = fetchedResultsController?.fetchedObjects
        let filt = (items! as NSArray).filteredArrayUsingPredicate(pred)

        let startCheckPoint:Checkpoint = (filt as NSArray).firstObject as! Checkpoint!
        let endCheckpoint:Checkpoint = (filt as NSArray).lastObject as! Checkpoint!

        if filt.count != 0 {

           if startCheckPoint .isEqual(annotation) {
              annotationView.pinTintColor = MKPinAnnotationView.greenPinColor()
           }else if endCheckpoint.isEqual(annotation) {

              annotationView.pinTintColor = MKPinAnnotationView.purplePinColor()
           }else {
              annotationView.pinTintColor = MKPinAnnotationView.redPinColor()

           }
     }
           let point = annotation as! Checkpoint

           annotationView.tag = (point.checkpoint_order?.integerValue)!
           annotationView.enabled = true
           annotationView.animatesDrop = true
   //  annotationView.canShowCallout = true
   annotationView.annotation = annotation
           // annotationView.opaque = false
           print("tag = \(annotationView.tag)")
  }
  return annotationView
 }

 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

  if !(view.annotation!.isKindOfClass(MKUserLocation)) {

     let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
     let pred2:NSPredicate = NSPredicate(format: "checkpoint_order == %d", view.tag)
     let compoundPredicate:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [pred, pred2])

     let items = fetchedResultsController?.fetchedObjects
     let filt = (items! as NSArray).filteredArrayUsingPredicate(compoundPredicate)
     let point:Checkpoint = (filt as NSArray).firstObject as! Checkpoint

     print(point.description)

        }

  }
 }

Upvotes: 1

Views: 1905

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

First of all, if you want to show callouts, you need to set canShowCallout to true. There are no callouts unless you enable them.

In a comment you said that you're getting a crash with an error that reads must implement title when canShowCallout is YES on corresponding view <MKPinAnnotationView: 0x7fb9ef536e70... What this is telling you is that the object you used as your annotation does not implement the title part of the MKAnnotation protocol. The title is an optional part of the protocol unless you use callouts in which case you must implement it. The title is what's displayed on the callout. The error message is telling you that you enabled the callout but didn't provide a title to display. You can either add title to the class you're using as the MKAnnotation or you can skip using callouts.

Upvotes: 2

Related Questions