簡體   English   中英

自定義注釋標注崩潰-幫助嗎?

[英]Custom Annotation Callout Crash — Help?

我已成功將自定義UILabel添加到注釋標注中,但是未能使UILabel顯示注釋.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打印annotation.title (錯誤提示:注釋對象不了解title方法)

因此,您將需要顯示更多代碼。 特別是在其中創建和添加這些批注的代碼,也可能是您的MKAnnotation的實現

上面的方法不是問題。

title是MKAnnotation協議的可選屬性。 並非每個注釋都具有該屬性,因此在確定該屬性可用之前,您無法設置或使用該屬性。 即使注釋具有該屬性,標准定義也不是可讀寫的,而是只讀的。

選項:

  1. 一旦測試了注釋的類並因此知道該屬性是可讀寫的,就將對該屬性的所有引用移到if / then塊中。
  2. 或者,使用if ([annotation respondsToSelector:@selector(setTitle:)])檢查讀/寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM