我有一個帶有自定義註釋的mapView。我還爲此添加了一個按鈕,但是有沒有辦法查看該按鈕在哪個註釋上被按下?Swift註解點擊按鈕
下面是代碼:
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")
}
當我點擊rightCalloutAccessoryView沒有任何反應。當我點擊標題或字幕標註時,也沒有任何反應。我做錯了什麼?
它工作!謝謝,但是我對這段代碼有錯誤:button.annotation = annotation錯誤:無法指定'MKAnnotation'類型的值來鍵入'CustomPointAnnotation?' 當然,我不知道點擊了什麼註釋。 – Dim
使用as將其轉換爲CustomPointAnnotation! –
謝謝。 button.annotation =註釋爲! CustomPointAnnotation – Dim