我宣布我的.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];
}
如果你看到任何错误,请指出来!
是您'annoForMoreDetails'对象实际上的类型'注释*'还是像'id'这样的东西? (它应该是后者。) –
Tim
2011-03-10 22:48:11
它是“<>”中带有MKAnnotation的NSObject的子类。所以类型注释* – 2011-03-10 22:50:41