你應該檢出MKMapKit委託文件,它有很多很好的方法可以用來完成你想要做的事情。我會絕對不嘗試並添加一個UIButton到註釋視圖。
管理註釋視圖 - MapView類:viewForAnnotation: - MapView類:didAddAnnotationViews: - MapView類:annotationView:calloutAccessoryControlTapped: 拖動註釋視圖 - MapView類:annotationView:didChangeDragState:fromOldState: 選擇註釋視圖 - MapView的:didSelectAnnotationView:
- MapView類:didDeselectAnnotationView:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass: [MKUserLocation class]])
{
return nil;
}
else if([annotation isKindOfClass:[MYCLASS class]])
{
static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
}
//Add an Image!
annotationView.image = [UIImage imageNamed:@"trashMarker.png"];
//Move the Frame Around!
[annotationView setFrame:CGRectMake(annotationView.frame.origin.x, annotationView.frame.origin.y - annotationView.frame.size.height, annotationView.frame.size.height, annotationView.frame.size.width)];
//Finally Set it as the annotation!
annotationView.annotation = annotation;
//Return the annotationView so the MKMapKit can display it!
return annotationView;
}}
你的MKAnnotation(IE符合協議)的子類默認應該包含一個標籤(我認爲),但是如果它不只是你自己添加一個屬性,所以你可以區分地圖上的不同標記。你的方法應該是這個樣子,
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
MYCLASS *selectedMapPin = view.annotation;
if(selectedMapPin.tag == MY_PIN_TAG)
{
//SHOW VIEW CONTROLLER
}}
更多的例子可以參考我們的開源項目綠起來佛蒙特州
Green Up Vermont iOS App
你能告訴什麼用第一時恰好不工作方案? – haawa