2

我在我的 pin 注释上添加了一个详细信息按钮。我想去另一个视图。当我单击它时,以下代码不起作用。我不知道问题出在哪里。

- (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];
}
}
4

1 回答 1

1

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

您还需要告诉班级您正在实现这些MKMapViewDelegate方法。为此,在您的 .h 中,更改此行:

@interface MyViewController : UIViewController

到 :

@interface MyViewController : UIViewController <MKMapViewDelegate>

您的方法现在应该被调用。

于 2012-11-22T08:53:23.270 回答