2016-07-25 18 views
1

Ich habe eine linke Taste zu meinem MKAnnotation hinzugefügt. Ich möchte, dass es dies ebenso zeigt wie die richtige Schaltfläche und die Bilder, die ich unten eingestellt habe. Dieser Code unten zeigt beide Tasten, erkennt aber nur die rechte Maustaste auf mein Segment für NewEntry. Wie kann die calloutAccessoryControl angegeben werden, um die linke Taste angezapft zu erkennen, sowie die rechte Taste angetippt, und fügen Sie den zusätzlichen Übergang von der linken Taste zu "links Detail" für meine LeftButtonDetailViewController?wie man vom 2. Knopf auf MKannotation übergeht?

Hier ist mein Code:

//MARK: - MKMapview Delegate 
extension MapViewController: MKMapViewDelegate { 

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

     guard let subtitle = annotation.subtitle! else { return nil } 

     if (annotation is SpecimenAnnotation) { 
      if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) { 
       return annotationView 
      } else { 
       let currentAnnotation = annotation as! SpecimenAnnotation 
       let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle) 

       switch subtitle { 
       case "Uncategorized": 
        annotationView.image = UIImage(named: "IconUncategorized") 
       case "Documentaries": 
        annotationView.image = UIImage(named: "IconDocumentaries") 
       default: 
        annotationView.image = UIImage(named: "IconUncategorized") 
       } 

       annotationView.enabled = true 
       annotationView.canShowCallout = true 

       let detailDisclosure1 = UIButton(type: UIButtonType.DetailDisclosure) 
       let detailDisclosure2 = UIButton(type: UIButtonType.InfoDark) 
       annotationView.rightCalloutAccessoryView = detailDisclosure1 
       annotationView.leftCalloutAccessoryView = detailDisclosure2 

       if currentAnnotation.title == "Empty" { 
        annotationView.draggable = true 
       } 

       return annotationView 
      } 
     } 
     return nil 
    } 

    func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) { 

     for annotationView in views { 
      if (annotationView.annotation is SpecimenAnnotation) { 
       annotationView.transform = CGAffineTransformMakeTranslation(0, -500) 
       UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: { 
        annotationView.transform = CGAffineTransformMakeTranslation(0, 0) 
        }, completion: nil) 
      } 
     } 

    } 

    func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     if let specimenAnnotation = annotationView.annotation as? SpecimenAnnotation { 
      performSegueWithIdentifier("NewEntry", sender: specimenAnnotation) 

     } 
    } 

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
     if newState == .Ending { 
      view.dragState = .None 
     } 
    } 

} 
+0

By the way, wenn 'dequeue' in' viewForAnnotation 'erfolgreich, bevor Sie die' annotationView' zurückgeben, müssen Sie die 'annotation' der Ansicht als 'annotation' festlegen, die an diese Methode übergeben wurde. Sie sollten auch jede andere notwendige Konfiguration durchführen, die sich für die neue "Annotation" ändern könnte (z. B. das Setzen des "ziehbaren" auf der Basis des "Titels"). – Rob

Antwort

1

Sie können das Zubehör zu den linken und rechten Zubehör vergleichen, um zu sehen, welche ausgewählt wurde:

func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    if let specimenAnnotation = annotationView.annotation as? SpecimenAnnotation { 
     if control == annotationView.leftCalloutAccessoryView { 
      // left 
     } else if control == annotationView.rightCalloutAccessoryView { 
      // right 
     } else { 
      fatalError("Unknown accessory") 
     } 
    } 
} 
+0

danke Rob ich werde das versuchen. Also füge ich in der nächsten Zeile hinzu, um nach der Zeile zu beginnen, die 'if control' startet? dh Einfügen der Zeilen performSegueWithIdentifier ("LeftDetail", Absender: sceneAnnotation) nach dieser Zeile und vor dem "else if" –

+0

Sie setzen einen 'performSegueWithIdentifier' wo es' 'left' steht und der andere wo' 'right' steht . – Rob

+0

Danke Rob! (Entschuldigung für meine verspätete Antwort. Hatte ein paar Bugs zu kämpfen, bevor ich das ausprobieren konnte). Es hat gut funktioniert! –