Iphone 自定义注释标注崩溃--帮助?

Iphone 自定义注释标注崩溃--帮助?,iphone,mapkit,mkannotationview,Iphone,Mapkit,Mkannotationview,我已成功将自定义UILabel添加到批注详图索引,但无法使UILabel显示annotation.title。该应用程序构建良好,但一旦我点击注释就会崩溃。它在下面代码的第一行与SIGABRT崩溃 注意:我使用的代码来自 我的代码在下面,谢谢 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { if (self.calloutAnnotation == ni

我已成功将自定义UILabel添加到批注详图索引,但无法使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))];
}

如果在方法中打印
annotation.title
的第一行代码上发生错误,则会看到该错误。(错误显示:注释对象不理解
标题
方法)

因此,您需要显示更多的代码。特别是创建和添加这些注释的代码,以及
MKAnnotation


上述方法不是问题。

title
是MKAnnotation协议的可选属性。并非每个批注都具有该属性,因此在确定该属性可用之前,无法设置或使用该属性。即使注释具有该属性,标准定义也不是读/写的,而是只读的

选项:

  • 一旦测试了注释的类并因此知道该属性是可读写可用的,则将对该属性的任何引用移动到if/then块中
  • 或者,使用
    if([annotation respondsToSelector:@selector(setTitle:)])
    检查读/写操作

  • 我更新了我的答案。。。仅供参考,在我添加
    -(MKAnnotationView*)mapView:(MKMapView*)mapView视图中的前三行之前,一切都很顺利Callout MapAnnotation是什么类?它有“所有权”属性吗?这就是问题所在,但您尚未显示其界面。callout MapAnnotation只是实际callout的视图。。。我添加了一个“tile”属性,它不再崩溃。。但是,它返回一个annotation.title的
    null
    。。。如何将customAnnotation.title连接到calloutAnnotation.title?
    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: