2016-08-28 2 views
0

私はカスタム注釈付きの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をクリックします。タイトルやサブタイトルのアノテーションをクリックしても何も起こりません。私は間違って何をしたの?

答えて

1

を設定する必要があることを忘れないでください注釈がクリックされたことを知っていると、サブクラス化する必要がありますUIButton

class MyButton : UIButton { 
    var annotation: CustomPointAnnotation? = nil 
} 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // ... 
    if anView == nil { 
     // ... 
     let button = MyButton(type: .DetailDisclosure) 
     button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside) 

     anView!.rightCalloutAccessoryView = button 
    } 

    if let button = anView!.rightCalloutAccessoryView as? MyButton { 
     button.annotation = annotation 
    } 

    return anView 
} 

func showAnnotationDisclosure(sender: MyButton) { 
    print("Disclosure button clicked") 
    print(sender.annotation?.title) 
} 
+0

ありがとうございますが、このコードでエラーが発生しました:button.annotation = annotationエラー:タイプ 'MKAnnotation'の値を 'CustomPointAnnotation'に割り当てることができません。 もちろん、注釈がクリックされたかどうかわかりません。 – Dim

+0

これを次のようにしてCustomPointAnnotationにキャストします。 –

+0

ありがとうございます。 button.annotation =注釈! CustomPointAnnotation – Dim

0

彼らはそうUIControl

の子孫である場合leftCalloutAccessoryViewまたはrightCalloutAccessoryViewを押圧するためのデリゲートメソッドのみが呼び出されますリンゴのドキュメントによると、どちらかそれを行うか、適切なビューにUITapGestureRecognizerの例えばインスタンスを追加することにより、ジェスチャー自分で処理。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // ... 

    if anView == nil { 
     // ... 
     let button = MyButton(type: .DetailDisclosure) 
     button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside) 

     anView!.rightCalloutAccessoryView = button 
    } 

    return anView 
} 

func showAnnotationDisclosure(sender: AnyObject) { 
    print("Disclosure button clicked") 
} 

あなたがしたい場合: はそれらがUIImageViewのインスタンスである場合はまた、あなたがそれを行う方法は、ボタンにターゲットを追加することですinstance.userInteractionEnabled = true

docs