我已经成功地将自定义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:发布于 2011-09-09 04:01:42
在打印annotation.title的方法的第一行代码中出现的错误。(错误提示:注释对象不理解title方法)
所以你需要展示更多的代码。具体地说,就是创建和添加这些注释的代码,可能还有MKAnnotation的实现
上面的方法不是问题所在。
发布于 2011-10-24 23:56:34
title是MKAnnotation协议的可选属性。并不是每个注释都具有该属性,因此在确定该属性可用之前,不能设置或使用该属性。即使注释具有该属性,标准定义也不是读/写的,而是只读的。
选项:
一旦您测试了注释类并因此知道该属性是读/写的,
if ([annotation respondsToSelector:@selector(setTitle:)])检查读/写。https://stackoverflow.com/questions/7353235
复制相似问题