2012-02-27 74 views
0

我有一個應用程序MKMapView和大量的針腳在這張地圖上。如何識別哪個銷被挖掘

每個銷釘都得到了rightCalloutAccessoryView。我用這種方式創建它:

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = rightButton; 

我應該怎麼知道,哪個針被點擊? Thnx

回答

10

showDetails:方法中,您可以從地圖視圖的selectedAnnotations數組中獲取引腳。雖然屬性是一個NSArray,剛剛得到的數組中的第一個項目,因爲地圖視圖只允許一次選中一個針:與其做addTarget和實施

//To be safe, may want to check that array has at least one item first. 

id<MKAnnotation> ann = [[mapView selectedAnnotations] objectAtIndex:0]; 

// OR if you have custom annotation class with other properties... 
// (in this case may also want to check class of object first) 

YourAnnotationClass *ann = [[mapView selectedAnnotations] objectAtIndex:0]; 

NSLog(@"ann.title = %@", ann.title); 


順便說一句,一種自定義方法,可以使用地圖視圖的calloutAccessoryControlTapped委託方法。竊聽註釋在view參數可用:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"ann.title = %@", view.annotation.title); 
} 

務必從viewForAnnotation刪除addTarget如果使用calloutAccessoryControlTapped

+1

如果2個註釋的標題相同,該怎麼辦? – 2015-09-23 09:40:37

+0

我知道我們可以繼承和做,有沒有更簡單的方法? – 2015-09-23 09:47:33

+0

@安娜,這真的幫助我..謝謝+1 – 2016-01-16 17:21:49