2014-09-03 27 views
1

我有一個mapView自定義註釋。我還爲此添加了一個按鈕,但是有沒有辦法查看該按鈕在哪個註釋上被按下?Swift Annotation CallOut按鈕選擇

所以發佈到控制檯不應該只是「Disclosure Pressed!」。我需要類似「Disclosure Pressed!info(X) .subtitle」來查看用戶錄製的info1或info2。

下面是代碼:

import UIKit 
import MapKit 

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var Map: MKMapView! 

    class CustomPointAnnotation: MKPointAnnotation { 
     var imageName: String! 
    } 

    @IBAction func btpressed(sender: AnyObject) { 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //1 
     var lat1:CLLocationDegrees = 40.748708 
     var long1:CLLocationDegrees = -73.985643 
     var latDelta1:CLLocationDegrees = 0.01 
     var longDelta1:CLLocationDegrees = 0.01 

     var span1:MKCoordinateSpan = MKCoordinateSpanMake(latDelta1, longDelta1) 
     var location1:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat1, long1) 
     var region1:MKCoordinateRegion = MKCoordinateRegionMake(location1, span1) 

     Map.setRegion(region1, animated: true) 

     var info1 = CustomPointAnnotation() 
     info1.coordinate = location1 
     info1.title = "Test Title1!" 
     info1.subtitle = "Subtitle1" 
     info1.imageName = "1.png" 

     Map.addAnnotation(info1) 

     //2 
     var lat2:CLLocationDegrees = 41.748708 
     var long2:CLLocationDegrees = -72.985643 
     var latDelta2:CLLocationDegrees = 0.01 
     var longDelta2:CLLocationDegrees = 0.01 

     var span2:MKCoordinateSpan = MKCoordinateSpanMake(latDelta2, longDelta2) 
     var location2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat2, long2) 
     var region2:MKCoordinateRegion = MKCoordinateRegionMake(location2, span2) 

     var info2 = CustomPointAnnotation() 
     info2.coordinate = location2 
     info2.title = "Test Title2!" 
     info2.subtitle = "Subtitle2" 
     info2.imageName = "2.png" 
     Map.addAnnotation(info2) 
    } 

    func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 

     if control == annotationView.rightCalloutAccessoryView { 
      println("Disclosure Pressed!") 
     } 
    } 

    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.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton 

      var imageview = UIImageView(frame: CGRectMake(0, 0, 10, 10)) 
      imageview.image = UIImage(named: "1.png") 
      anView!.leftCalloutAccessoryView = imageview 
     } else { 
      anView.annotation = annotation 
     } 

     //Set annotation-specific properties **AFTER** 
     //the view is dequeued or created... 

     let cpa = annotation as CustomPointAnnotation 
     anView.image = UIImage(named:cpa.imageName) 

     return anView 
    } 

} 

我已經嘗試過這樣的:

if control == annotationView.rightCalloutAccessoryView { 
    println("Disclosure Pressed!" \(self.subtitle)) 
} 

回答

6

calloutAccessoryControlTapped委託方法,self不是其標註附件視圖被竊聽的註解。

self指的是方法所包含的類的實例(即ViewController的實例)。另一個問題是在println中,您將變量引用放在引號之外而不是內部。


委託方法爲您提供了對註釋視圖的引用:annotationView參數。

註解視圖包含對註釋的引用:annotationView.annotation


但是,請注意您的委託方法聲明從一個文件(annotationView說法有一個本地名稱view等論調被標記爲可選與!後綴)略有不同。

即使您的版本在技術上仍然有效,最好使用記錄的聲明。

下面還

完整的示例演示如何檢查註釋您的自定義對象之一以及如何訪問自定義屬性:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
      calloutAccessoryControlTapped control: UIControl!) { 

    if control == view.rightCalloutAccessoryView { 
     println("Disclosure Pressed! \(view.annotation.subtitle)") 

     if let cpa = view.annotation as? CustomPointAnnotation { 
      println("cpa.imageName = \(cpa.imageName)") 
     } 
    } 

} 


旁註:
既然你已經設置的leftCalloutAccessoryViewUIImageView,點擊它將而不是請致電calloutAccessoryControlTapped,因爲它只被調用爲UIControl的子類的視圖。 UIImageView不是UIControl的子類。如果您需要檢測左側附件上的水龍頭,請創建自定義UIButton(並設置其圖像)而不是UIImageView