为什么我使用MapViewController将无法识别的选择器发送到实例异常? (IOS)

时间:2012-08-14 18:35:19

标签: objective-c ios unrecognized-selector cs193p

我正在参加斯坦福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.
}

2 个答案:

答案 0 :(得分:5)

如果您传递的参数实际上不是-[NSConcreteData _isResizable]: unrecognized selector sent to instance,则在setImage上调用UIImageView时可能会发生错误UIImage

根据您的评论,getImageForMapViewController方法实际上正在返回NSData而不是UIImage。这可能会导致您看到的错误。

修复getImageForMapViewController方法以返回UIImage

答案 1 :(得分:1)

如果您需要更改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。您正尝试在image上设置UIViewUIView不响应setImage方法。设置图像后,您尝试将UIView投射到UIImageView,这是一个坏习惯。因此,您需要将图像添加到imageView,然后才需要将imageView指定为leftCalloutAccessoryView

当您尝试像这样写[(UIImageView *)view.leftCalloutAccessoryView setImage:image];时,请记得先将其强制转换,然后调用该方法。对于上面的行,最好写一下,

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