私はカスタム注釈付きの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
これを次のようにしてCustomPointAnnotationにキャストします。 –
ありがとうございます。 button.annotation =注釈! CustomPointAnnotation – Dim