2016-07-25 26 views
1

我在我的MKAnnotation上添加了一个左键。我希望它能够显示这个以及我在下面设置的右键和图像。下面的代码显示了这两个按钮,但只识别右键点击我的segue NewEntry。如何指定calloutAccessoryControl来识别点击左键,以及点击右键,并从左边的按钮添加额外的segue到左边的细节为我的LeftButtonDetailViewController如何从MKannotation上的第二个按钮继续?

这里是我的代码:

//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

顺便说一句,如果'dequeue'在' viewForAnnotation'成功后,在返回'annotationView'之前,必须将视图的'annotation'设置为传递给此方法的'annotation'。您还应该执行其他必要的配置,这些配置可能会针对新的'annotation'进行更改(例如,在'title'的基础上设置'draggable')。 – Rob

回答

1

您可以将附件比较左,右附件,看看哪些选择:

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

谢谢罗布我会试试这个。那么我应该在下一行添加以在'if control'开始的行后继续?即在该行之后和'else if'之前插入行performSegueWithIdentifier(“LeftDetail”,sender:specimenAnnotation) –

+0

您在其中指出'// left'和另一个表示'// right'的地方放了一个'performSegueWithIdentifier', 。 – Rob

+0

谢谢Rob! (对于我的延迟答复表示歉意,在我能够尝试这个之前有一些错误的战斗)。它运作良好! –

相关问题