簡體   English   中英

為什么我的MapViewController收到無法識別的選擇器發送到實例異常? (iOS)

[英]Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS)

我正在參加Stanford iOS課(對不起,我是其中之一,但我想一定要學點知識),並且我使用的代碼與教授在MKMapViews講座中使用的代碼幾乎完全相同,但是我得到了他沒有的例外,我真的無法弄清楚。 是什么原因造成的?

我得到的例外:

-[NSConcreteData _isResizable]:無法識別的選擇器已發送到實例0x90a4c00

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!aView) {
        aView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        aView.canShowCallout=YES;
        aView.leftCalloutAccessoryView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        aView.rightCalloutAccessoryView= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    aView.annotation=annotation;
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
    return aView;
}

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image]; // this is where I get the exception.
}

如果傳遞的參數不是UIImage則在UIImageView上調用setImage時,可能會發生錯誤-[NSConcreteData _isResizable]: unrecognized selector sent to instance

根據您的評論, getImageForMapViewController方法實際上返回NSData而不是UIImage 這可能會導致您看到的錯誤。

修復getImageForMapViewController方法以返回UIImage

如果您需要更改MKPinAnnotationView的圖像,請使用以下命令:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MKAnnotation *pin = view.annotation;
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];

    UIImageView *imagePin = [[UIImageView alloc] initWithImage:image];
   [[mapView viewForAnnotation:pin] addSubview:imagePin];
}

這是問題,更改此方法:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image]; // this is where I get the exception.
}

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    view.leftCalloutAccessoryView = imageView; // this is where I get the exception.
}

這里的問題是leftCalloutAccessoryView的類型為UIView 您正在嘗試在UIView上設置image UIView不響應setImage方法。 設置圖像后,您嘗試將UIViewUIImageView ,這是一個壞習慣。 因此,您需要在將圖像添加到leftCalloutAccessoryView之后,將leftCalloutAccessoryView分配為leftCalloutAccessoryView

當您嘗試這樣寫時[(UIImageView *)view.leftCalloutAccessoryView setImage:image]; 請記住先進行轉換,然后再調用方法。 對於上面的行,最好這樣寫:

UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];

暫無
暫無

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

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