在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
。
如果2個註釋的標題相同,該怎麼辦? – 2015-09-23 09:40:37
我知道我們可以繼承和做,有沒有更簡單的方法? – 2015-09-23 09:47:33
@安娜,這真的幫助我..謝謝+1 – 2016-01-16 17:21:49