Я успешно добавил пользовательский 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: