【发布时间】:2016-08-28 09:02:19
【问题描述】:
我有一个带有自定义注释的 mapView。我还为此添加了一个按钮,但有没有办法查看按钮是在哪个注释上按下的?
代码如下:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.canShowCallout = true
anView!.enabled = true
anView?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
let imageview_left = UIImageView(frame: CGRectMake(0, 0, 20, 20))
imageview_left.image = UIImage(named: "left.png")
anView?.leftCalloutAccessoryView = imageview_left
let imageview_right = UIImageView(frame: CGRectMake(0, 0, 8, 13))
imageview_right.image = UIImage(named: "right.png")
anView!.rightCalloutAccessoryView = imageview_right
}
else {
anView!.annotation = annotation
}
let subtitleView = UILabel()
subtitleView.font = UIFont(name: "GillSans-Light", size: 14)
subtitleView.numberOfLines = 1
subtitleView.text = annotation.subtitle!
anView!.detailCalloutAccessoryView = subtitleView
let cpa = annotation as! CustomPointAnnotation
anView!.image = UIImage(named:cpa.imageName)
return anView
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView {
print("Pressed!")
}
print("Click")
}
当我点击右边的CalloutAccessoryView 时,什么也没有发生。当我单击标题或副标题注释时,也没有任何反应。我做错了什么?
【问题讨论】:
标签: ios swift mkmapview mkannotation mkannotationview