单击地图注释时显示另一个视图

adr*_*ian 9 iphone annotations map

我有一个只有一个注释的地图.我创建了一个简单的类,我希望它在用户点击注释时显示.问题是当我点击注释时没有任何反应.

这是我的代码:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    NSLog(@"Reverse Geocoder completed");
    mPlacemark=placemark;
    [mapView addAnnotation:placemark];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.animatesDrop=TRUE;

    //create UIButton for annotation
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView=detailButton;
    return annView;
}


-(void)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
Run Code Online (Sandbox Code Playgroud)

我的showDetailView功能永远不会被调用.请帮助我.我是iphone的新手,我可能会忘记一件简单的事情.谢谢

还是行不通!!!!

小智 16

首先,检查delegate是否设置了地图视图,否则viewForAnnotation将不会调用您的方法.

接下来,附件按钮出现在注释的标注上,只有在您设置时才会出现canShowCallout:

annView.canShowCallout = YES;
Run Code Online (Sandbox Code Playgroud)

接下来,不是使用自己的方法来处理按钮操作,而是使用地图视图自己的calloutAccessoryControlTapped委托方法,这种方法在点击附件时会被调用:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"inside the stupid method");

    //Here, the annotation tapped can be accessed using view.annotation
}
Run Code Online (Sandbox Code Playgroud)

[detailButton addTarget...从中删除该行viewForAnnotation.

另请注意,您NSLog在showDetailView方法中缺少前导@,这会在调用方法时导致崩溃.

另一件事是您应该使用dequeueReusableAnnotationViewWithIdentifierin viewForAnnotation来启用注释视图重用.


请求示例:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    static NSString *annReuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

        annView.animatesDrop = YES;
        annView.canShowCallout = YES;

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.rightCalloutAccessoryView=detailButton;
    }
    else {
        annView.annotation = annotation;
    }

    return annView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
Run Code Online (Sandbox Code Playgroud)