我已成功将自定义UILabel添加到注释标注中,但未能使UILabel显示annotation.title。该应用程序构建良好,但一旦我点击注释崩溃。它在下面代码的第一行与SIGABRT崩溃。
注意:我正在使用Asynchrony Solutions中的代码。
我的代码在下面,谢谢!
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation == nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
[self.mapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation &&
view.annotation == self.customAnnotation &&
!((BasicMapAnnotationView *)view).preventSelectionChange) {
[self.mapView removeAnnotation: self.calloutAnnotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(@"Ann Title: %@", annotation.title);
theAnnTitle = annotation.title;
NSLog(@"Ann Title 2: %@", theAnnTitle);
if (annotation == self.calloutAnnotation) {
CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
if (!calloutMapAnnotationView) {
calloutMapAnnotationView = [[[AccessorizedCalloutMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 70.0f;
UILabel *annTitle = [[[UILabel alloc] init] autorelease];
annTitle.frame = CGRectMake(0, 0, 200, 50);
annTitle.backgroundColor = [UIColor clearColor];
annTitle.textColor = [UIColor whiteColor];
annTitle.text = theAnnTitle;
//NSLog(@"Ann Title 3: %@", theAnnTitle);
[calloutMapAnnotationView.contentView addSubview:annTitle];
}
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
} else if (annotation == self.customAnnotation) {
MKPinAnnotationView *annotationView = [[[BasicMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorGreen;
return annotationView;
} else if (annotation == self.normalAnnotation) {
MKPinAnnotationView *annotationView = [[[BasicMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"NormalAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorPurple;
return annotationView;
} else {
MKPinAnnotationView *annotationView = [[[BasicMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
return nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:38.6335 andLongitude:-90.2045] autorelease];
self.customAnnotation.title = @"I AM ANGRYY!";
[self.mapView addAnnotation:self.customAnnotation];
self.normalAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:38 andLongitude:-90.2045] autorelease];
self.normalAnnotation.title = @"I AM HAPPY";
[self.mapView addAnnotation:self.normalAnnotation];
BasicMapAnnotation *behindCalloutAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:38.9 andLongitude:-89.5] autorelease];
behindCalloutAnnotation.title = @"I Am Selected.";
[self.mapView addAnnotation:behindCalloutAnnotation];
CLLocationCoordinate2D coordinate = {38.315, -90.2045};
[self.mapView setRegion:MKCoordinateRegionMake(coordinate,
MKCoordinateSpanMake(1, 1))];
}
崩溃日志:
2011-09-08 12:07:05.735 CustomMapAnnotationExample[24648:e903] -[CalloutMapAnnotation title]: unrecognized selector sent to instance 0xc256a00
2011-09-08 12:07:05.736 CustomMapAnnotationExample[24648:e903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalloutMapAnnotation title]: unrecognized selector sent to instance 0xc256a00'
*** Call stack at first throw:
答案 0 :(得分:1)
在您打印annotation.title
的方法的第一行代码中发生的错误。 (错误说明:注释对象不理解title
方法)
因此您需要显示更多代码。特别是您创建和添加这些注释的代码,也可能是您MKAnnotation
上述方法不是问题。
答案 1 :(得分:0)
title
是MKAnnotation协议的可选属性。并非每个注释都具有该属性,因此在您确定该属性可用之前,您无法设置或使用该属性。即使注释具有该属性,标准定义也不会读/写它是只读的。
选项:
if ([annotation respondsToSelector:@selector(setTitle:)])
检查读/写。