2016-07-25 1 views
1

Я добавил левую кнопку к моему MKAnnotation. Я хочу, чтобы он показывал это, а также правую кнопку и изображения, которые я установил ниже. Этот код ниже показывает обе кнопки, но только распознает правую кнопку, нажмите на мой сегмент для NewEntry. Как указать calloutAccessoryControl, чтобы распознать левую кнопку нажатой, а также правую кнопку нажатой, и добавить дополнительный переход от левой кнопки к «левой детали» для моего LeftButtonDetailViewController?как перейти от 2-й кнопки на 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», вы должны установить «аннотацию» представления как «аннотацию», которая была передана этому методу. Вы также должны выполнить любую другую необходимую конфигурацию, которая может измениться для новой «аннотации» (например, установка «draggable» на основе «title»). – 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»? т.е. вставка строк выполняетSegueWithIdentifier («LeftDetail», отправитель: образецAnnotation) после этой строки и перед «else if» –

+0

Вы помещаете один 'performSegueWithIdentifier', где он говорит' // left', а другой, где он говорит '// right', , – Rob

+0

Спасибо, Роб! (приношу извинения за ответную задержку. Было несколько ошибок для битвы, прежде чем я смог попробовать это). Это сработало! –

 Смежные вопросы

  • Нет связанных вопросов^_^