单击pin注释时如何显示另一个视图

时间:2012-11-22 08:30:06

标签: iphone ios xcode ipad mapkit

我在我的图钉注释上添加了一个细节按钮。我想去另一个观点。当我点击它时,以下代码不起作用。我不知道问题出在哪里。

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != mapview.userLocation)
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }

    return pinAnnotation;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
    annotationViewController *detailView=[[annotationViewController alloc] initWithNibName:@"annotationViewController" bundle:nil];

    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
}

1 个答案:

答案 0 :(得分:1)

方法- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control是委托方法。这就是为什么您需要将mapview的委托设置为实现此方法的类(此处为self),以便调用它。

您还需要告诉班级您正在实施MKMapViewDelegate方法。为此,在你.h中,改变这一行:

@interface MyViewController : UIViewController

致:

@interface MyViewController : UIViewController <MKMapViewDelegate>

现在应该调用你的方法。