自定义标注标注崩溃——帮助



我已经成功地将自定义UILabel添加到注释callout中,但未能使UILabel显示annotation.title。这款应用构建得很好,但一旦我点击一个注释就会崩溃。它在下面的第一行代码中使用SIGABRT崩溃。

注意:我正在使用异步解决方案的代码。

我的代码如下,谢谢!

- (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:

您看到的错误发生在您打印annotation.title的方法的第一行代码上。(错误提示:注释对象不理解title方法)

所以你需要显示更多的代码。特别是创建和添加注释的代码以及MKAnnotation

的实现

title是MKAnnotation协议的可选属性。不是每个注释都有那个属性,所以在确定属性可用之前,不能设置或使用那个属性。即使注释具有该属性,标准定义也不是读/写,而是只读。

选项:

  1. 将对该属性的任何引用移动到if/then块中,一旦你测试了注释的类,从而知道该属性是读写可用的。
  2. 或者,使用if ([annotation respondsToSelector:@selector(setTitle:)])来检查读写。

最新更新