2011-03-10 80 views
0

我宣布我的.h文件中的以下内容:获取错误注释从MapView的委托方法

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

如果你看到任何错误,请指出来!

+0

是您'annoForMoreDetails'对象实际上的类型'注释*'还是像'id '这样的东西? (它应该是后者。) – Tim 2011-03-10 22:48:11

+0

它是“<>”中带有MKAnnotation的NSObject的子类。所以类型注释* – 2011-03-10 22:50:41

回答

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... 
} 
+0

非常感谢!它完美地工作,你完全明白我想要的东西! – 2011-03-10 23:04:54