简体   繁体   English

为什么我的MapViewController收到无法识别的选择器发送到实例异常? (iOS)

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

I'm taking the Stanford iOS class (sorry, I'm one of these guys but gotta learn somehow I guess) and I'm using code that is almost exactly the same as the code that the prof used in the lecture about MKMapViews but I'm getting this exception that he didn't and I really can't figure it out. 我正在参加Stanford iOS课(对不起,我是其中之一,但我想一定要学点知识),并且我使用的代码与教授在MKMapViews讲座中使用的代码几乎完全相同,但是我得到了他没有的例外,我真的无法弄清楚。 What could be causing this? 是什么原因造成的?

The exception I'm getting: 我得到的例外:

-[NSConcreteData _isResizable]: unrecognized selector sent to instance 0x90a4c00 -[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.
}

The error -[NSConcreteData _isResizable]: unrecognized selector sent to instance can happen when calling setImage on a UIImageView if the parameter you are passing is not really a UIImage . 如果传递的参数不是UIImage则在UIImageView上调用setImage时,可能会发生错误-[NSConcreteData _isResizable]: unrecognized selector sent to instance

According to your comment, the getImageForMapViewController method is actually returning NSData instead of a UIImage . 根据您的评论, getImageForMapViewController方法实际上返回NSData而不是UIImage This can cause the error you are seeing. 这可能会导致您看到的错误。

Fix the getImageForMapViewController method to return a UIImage . 修复getImageForMapViewController方法以返回UIImage

If you need to change the image of your MKPinAnnotationView use like: 如果您需要更改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];
}

Here is the issue, Change this method: 这是问题,更改此方法:

-(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.
}

to

-(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.
}

Here the issue is that the leftCalloutAccessoryView is of type UIView . 这里的问题是leftCalloutAccessoryView的类型为UIView You are trying to set image on UIView . 您正在尝试在UIView上设置image UIView don't respond to setImage method. UIView不响应setImage方法。 After setting the image you are trying to cast UIView to UIImageView , that's a bad habit. 设置图像后,您尝试将UIViewUIImageView ,这是一个坏习惯。 So you need to add the image to a imageView after that you need to assign the imageView as the leftCalloutAccessoryView . 因此,您需要在将图像添加到leftCalloutAccessoryView之后,将leftCalloutAccessoryView分配为leftCalloutAccessoryView

When you are trying to write like this [(UIImageView *)view.leftCalloutAccessoryView setImage:image]; 当您尝试这样写时[(UIImageView *)view.leftCalloutAccessoryView setImage:image]; remember to cast it first then call the method. 请记住先进行转换,然后再调用方法。 For the above line it's better to write like, 对于上面的行,最好这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到一个“无法识别的选择器发送到实例”错误? - Why am I getting a `unrecognized selector sent to instance` error? 为什么我会收到“ [__NSArrayI allKeys]:无法识别的选择器发送到实例” /为什么要进行NSDictionary转换? - Why am I getting “[__NSArrayI allKeys]: unrecognized selector sent to instance” / why is NSDictionary transforming? 无法识别的选择器发送到iOS7中的实例异常 - unrecognized selector sent to instance exception in iOS7 为什么我收到此错误? CCSprite copyWithZone无法识别的选择器发送到实例 - Why am I getting this error? CCSprite copyWithZone unrecognized selector sent to instance 引发iOS异常:无法识别的选择器已发送到实例 - iOS Exception was thrown: unrecognized selector sent to instance 无法识别的选择器已发送到实例ios - unrecognized selector sent to instance ios 异常:无法识别的选择器发送到实例,在IOS 8中关闭视图控制器 - Exception :unrecognized selector sent to instance Dismissing View Controller in IOS 8 无法识别的选择器发送到实例 - 为什么? - Unrecognized selector sent to instance - why? 为什么会出现“无法识别的选择器”错误? - Why am I getting “unrecognized selector” errors? 为什么我得到这个:_cfurl:无法识别的选择器 - Why am I getting this: _cfurl: unrecognized selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM