我正在使用MKMapView對象,並且能夠創建引腳並將它們放置在地圖上。我現在想要做的是從引腳獲取信息。例如在我的如何從iOS中的地圖引腳獲取位置
- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation (id<MKAnnotation>)annotation
我創建我的針腳並將其設置在地圖上。我也正在創建按鈕以供用戶點擊該引腳時使用。所有這一切都很好,很好。現在我的問題是,當按鈕被點擊時,我如何從點擊的引腳獲取信息?以下是我目前如何設置的一小段示例代碼。任何建議將不勝感激。
- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop = YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
UIButton* callout = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[callout addTarget:self action:@selector(loadDetails:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = callout;
return annView;
}
- (void)loadDetails:(id)sender
{
//TODO: Get pin coordinates or address and match it back to an array of available address to show details in a new view
}
任何意見是極大的讚賞
這對我來說很好。唯一的問題是我有這個還必須創建按鈕嗎?我覺得如果按鈕沒有被設置或者其他任何東西都是浪費的。 – Seb 2012-04-16 20:43:58
我認爲你必須使用'[UIButton buttonWithType:UIButtonTypeDetailDisclosureDisclosure]'來創建一個按鈕,但是你不必設置一個目標/動作。它在表格視圖中的設置與詳細披露不同。嘗試它並在這裏評論回來,如果它的工作。 – 2012-04-16 20:54:07
只需創建按鈕並設置標註即可。再次感謝你。 – Seb 2012-04-16 20:55:37