将Disclosure Button添加到MKPointAnnotation

时间:2012-06-07 19:23:24

标签: ios

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

    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];

效果很好但是我想添加一个公开按钮,这样我就可以将seque推到新的视图控制器并显示图像。这可能吗?

提前,

- BD -

2 个答案:

答案 0 :(得分:9)

将您的班级声明为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-files ...)

答案 1 :(得分:2)

Axel的回答是正确的(我刚刚赞成):您必须实现MKMapViewDelegate并将其实例分配给MKMapView的Delegate属性。对于使用 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.
    }
}