Aby to osiągnąć, musisz dodać cel dla odpowiedniego widoku akcesoriów. Możesz to osiągnąć, ustawiając przycisk na rightCalloutAccessoryView, jak pokazano w fragmencie kodu.
class MapViewController: UIViewController, MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is Annotation {
let annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier")
let rightButton = UIButton(type: .DetailDisclosure)
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(_:)), forControlEvents: .TouchUpInside)
annotationView.rightCalloutAccessoryView = rightButton
}
return nil
}
func didClickDetailDisclosure(button: UIButton) {
// TODO: Perform action when was clicked on right callout accessory view.
}
}
// Helper classes.
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
class AnnotationView: MKAnnotationView {
}
Dzięki za odpowiedź. Nie ma sposobu, aby użyć tylko metody delegate calloutAccessoryControlTapped? –
Nie wiem inaczej. Jeśli zajrzysz do kodu źródłowego Apple, robią to w ten sam sposób. – Ramis