2016-08-28 44 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

你這樣做是爲了一個目標添加到該按鈕的方式:

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") 
} 

現在,如果你想知道點擊了什麼註釋,你必須子類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

使用as將其轉換爲CustomPointAnnotation! –

+0

謝謝。 button.annotation =註釋爲! CustomPointAnnotation – Dim

0

根據蘋果的文檔,如果他們的UIControl

所以子孫按leftCalloutAccessoryViewrightCalloutAccessoryView委託方法纔會被調用要麼做,或通過添加的UITapGestureRecognizer如實例相應視圖自己處理手勢。 記住,如果這些是UIImageView情況下,您還可以設置instance.userInteractionEnabled = true

docs