我正在尝试在Swift中使用Map Kit。我尝试在地图上显示该区域,一个图钉(MKPinAnnotationView)和当前位置。所有显示正常。我尝试添加“披露按钮”并拦截其点击。添加了“披露”按钮,但无法进行拦截窃听。
pinPressed具有方法的功能calloutAccessoryControlTapped不起作用。
pinPressed
calloutAccessoryControlTapped
这是一个示例代码:
import UIKit import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var mainMapView: MKMapView! var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() var objectLatitude = 53.204526 var objectLongitude = 50.111751 var currentLatitude = 53.203715 var currentLongitude = 50.160374 var latDelta = 0.05 var longDelta = 0.05 var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta) var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude) var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan) self.mainMapView.setRegion(currentRegion, animated: true) var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude) var objectAnnotation = MKPointAnnotation() objectAnnotation.coordinate = objectLocation objectAnnotation.title = "St. George's Church" objectAnnotation.subtitle = "Church of the Great Martyr St. George" self.mainMapView.addAnnotation(objectAnnotation) } func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if annotation is MKUserLocation { //return nil so map view draws "blue dot" for standard user location return nil } let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) pinView!.canShowCallout = true pinView!.animatesDrop = true pinView!.pinColor = .Purple pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton } else { pinView!.annotation = annotation } return pinView } func pinPressed(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if control == annotationView.rightCalloutAccessoryView { println("Disclosure Pressed!") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
该calloutAccessoryControlTapped委托方法必须命名mapView(annotationView:calloutAccessoryControlTapped:)。
mapView(annotationView:calloutAccessoryControlTapped:)
您不能使用自己的名字,例如pinPressed(...)。
pinPressed(...)
这适用于任何委托方法,并由协议规定。
所以应该是:
func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if control == annotationView.rightCalloutAccessoryView { println("Disclosure Pressed!") } }