将披露button添加到MKPointAnnotation

为了在故事板iOS项目中创build地图注释,我使用了:

CLLocationCoordinate2D annotationCoord3; annotationCoord3.latitude = 34.233129; annotationCoord3.longitude = -118.998644; MKPointAnnotation *annotationPoint3 = [[MKPointAnnotation alloc] init]; annotationPoint3.coordinate = annotationCoord3; annotationPoint3.title = @"Another Spot"; annotationPoint3.subtitle = @"More than a Fluke"; [_mapView addAnnotation:annotationPoint3]; 

它工作的很好,但我想添加一个揭示button,所以我可以推seque到一个新的视图控制器和显示图像。 这可能吗?

Thx提前,

–bd–

声明你的类是一个MKMapViewDelegate 。 然后加

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"]; if(!annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"]; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; } annotationView.enabled = YES; annotationView.canShowCallout = YES; return annotationView; } 

然后你添加:

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { // Go to edit view ViewController *detailViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; [self.navigationController pushViewController:detailViewController animated:YES]; } 

… ViewController可以是你定义的任何东西(我使用nib文件…)

Axel的答案是正确的(我只是upvoted):你必须实现MKMapViewDelegate并将其实例分配给MKMapViewDelegateDelegate属性。 对于那些使用MonoTouch的人来说 ,以下是端口:

 class MapDelegate : MKMapViewDelegate { public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation) { MKAnnotationView annotationView = mapView.DequeueReusableAnnotation ("String"); if (annotationView == null) { annotationView = new MKAnnotationView(annotation, "String"); annotationView.RightCalloutAccessoryView = new UIButton(UIButtonType.DetailDisclosure); } annotationView.Enabled = true; annotationView.CanShowCallout = true; return annotationView; } public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control) { // Push new controller to root navigation controller. } }