从MapView Delegate方法获取错误的注释

时间:2011-03-10 22:44:22

标签: iphone objective-c ios4 mkmapview

我在.h文件中声明了以下内容:

Annotation *annoForMoreDetails

然而,当我尝试将其设置为方法

中的当前注释时

- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation

它被设置为错误的对象。

这是我的代码:

 - (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation
{
    NSLog(@"welcome into the map view annotation");
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    for (annotation in [mapView annotations]) {
        if ([[mapView annotations] containsObject:annotation]) {
            annoForMoreDetails = annotation;
        }
    }
    if (annoForMoreDetails.coordinate.latitude != mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude != mapViews.userLocation.coordinate.longitude) {
    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor = MKPinAnnotationColorGreen;

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(moreDetails)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;
    return pinView;
    }
    else if (annoForMoreDetails.coordinate.latitude == mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude == mapViews.userLocation.coordinate.longitude) {
        return nil;
    }
    return nil;
}

- (void)moreDetails {
    annotationCalloutView.frame = CGRectMake(70, 120, 188, 218);
    annotationCalloutView.alpha = 0.0;
    mapView.userInteractionEnabled = NO;
    annotationCalloutView.userInteractionEnabled = YES;
    titleLabel.text = annoForMoreDetails.title;
    titleLabel.numberOfLines = 0;
    titleLabel.userInteractionEnabled = NO;
    addressLabel.text = annoForMoreDetails.subtitle;
    [self.view addSubview:annotationCalloutView];
    [UIView beginAnimations:@"callout" context:NULL];
    [UIView setAnimationDuration:0.6];
    annotationCalloutView.alpha = 1.0;
    [UIView commitAnimations];
}

如果您发现任何错误,请指出!

1 个答案:

答案 0 :(得分:1)

您是否尝试使用annoForMoreDetails来了解用户点击的注释,以便显示更多详细信息?如果是这样的话,那就有一种更简单的方法。使用地图视图的calloutAccessoryControlTapped委托方法。当用户点击附件按钮时它将被调用,它将通过包含注释作为属性的注释视图。

删除annoForMoreDetails实例变量,并在viewForAnnotation中,删除设置annoForMoreDetails的for循环(我认为它最终会将其设置为每次最后一次注释)。删除对annoForMoreDetails的所有其他引用。

同样在viewForAnnotation中,删除rightButton上的addTarget行,因为您将使用calloutAccessoryControlTapped的实现替换自定义moreDetails方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    Annotation *annoForMoreDetails = (Annotation *)view.annotation;

    //the code currently in moreDetails method goes here...
}