iPhone Dev = 地图和取消选择注释

发布于 2024-08-09 22:09:20 字数 760 浏览 2 评论 0原文

我使用一系列注释成功在地图上绘制注释。我什至可以单击注释并更改其颜色或图像。当用户选择第二个注释并且我想动态地将第一个注释的颜色或图像更改回未选择的颜色/图像时,我的问题出现了。我可以获取所有注释的数组并处理该数组,但是一旦我尝试设置数组的颜色或图像,我就会收到类似的错误。

for (MKAnnotationView *ann in map.selectedAnnotations){ 
  if ([ann isMemberOfClass:[Place class]]) { 
    place = (Place *)ann; 
      if (currentPlaceID != place.placeID) { 
        UIImage *i = [UIImage imageNamed:@"pin.png"];
        ann.image = i; 
      }
}

上面的代码工作正常,直到我到达 ann.image = i;然后就出错了。我得到的错误是:-

  • -[Place setImage:]:无法识别的选择器发送到实例 0x4514370 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'** -[放置 setImage:]: 无法识别的选择器发送到实例 0x4514370'

请告知,因为我已经在这个问题上绕了 2 天了!!!!

关于如何最好地做到这一点有什么想法吗?

提前致谢

I am successfully drawing annotations on a map using an array of annotations. I can even click on the annotation and change it’s colour or image. My problem arises when the use selects the second annotation and I want to dynamically change the colour or image of the first one back to a non-selected colour/image. I can get the array of all the annotations and work through the array but once I try to set the colour or image ot the array I get a similar error.

for (MKAnnotationView *ann in map.selectedAnnotations){ 
  if ([ann isMemberOfClass:[Place class]]) { 
    place = (Place *)ann; 
      if (currentPlaceID != place.placeID) { 
        UIImage *i = [UIImage imageNamed:@"pin.png"];
        ann.image = i; 
      }
}

the above code works ok until I get to ann.image = i; then it errors. The errors I get are:-

  • -[Place setImage:]: unrecognized selector sent to instance 0x4514370 Terminating app due to uncaught exception
    'NSInvalidArgumentException', reason: '** -[Place setImage:]:
    unrecognized selector sent to instance 0x4514370'

Please advise as I have been going around in circles on this one for 2 days now!!!!

Any ideas on how best to do this?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-08-16 22:09:20

你在 Place 类上有一个名为 image 的属性吗?

像... @property (nonatomic, keep) UIImage* image; 它是否正确合成? @synthesize 图像;

该错误非常简单,某些对象正在接收一条它不响应的消息,即由 .image 调用的“setImage”。

这是你的代码:

1. for (MKAnnotationView *ann in map.selectedAnnotations) {
2.    if ([ann isMemberOfClass:[Place class]]) {
3.        place = (Place *)ann;
4.        if (currentPlaceID != place.placeID) {
5.           UIImage *i = [UIImage imageNamed:@"pin.png"];
6.           ann.image = i;
7.        }
8.    }
9. }

我可以看到:

  • ann是一个MKAnnotationView(来自map.selectedAnnotations)
  • 你正在将你的注释类型转换到第3行的一个地方(这是对的吗?放置子类MKAnnotationView吗?
  • )正确地将图像设置为注释

这意味着什么:

  • 如果 Place 确实是 MKAnnotationView 的子类,则您隐藏了 setImage(以某种方式)方法
  • 如果 Place 不是 MKAnnotationView 的子类,则您已向注释中添加了无效注释(当然)您试图将其视为注释。

Do you have a property on the class Place called image?

Something like... @property (nonatomic, retain) UIImage* image; and is it properly synthesized? @synthesize image;?

The error is pretty straight forward, some object is receiving a message that it doesn't respond to, namely 'setImage' which is invoked by the .image.

Here is your code:

1. for (MKAnnotationView *ann in map.selectedAnnotations) {
2.    if ([ann isMemberOfClass:[Place class]]) {
3.        place = (Place *)ann;
4.        if (currentPlaceID != place.placeID) {
5.           UIImage *i = [UIImage imageNamed:@"pin.png"];
6.           ann.image = i;
7.        }
8.    }
9. }

What I can see:

  • ann is an MKAnnotationView (from map.selectedAnnotations)
  • you are typecasting your annotation to a place on line 3 (is this right? Does Place subclass MKAnnotationView?)
  • you are properly setting the image to the annotation

What this means:

  • If Place is indeed a subclass of MKAnnotationView, you hid the setImage (somehow) method
  • If Place is NOT a subclass of MKAnnotationView, you've added an invalid annotation to the annotations (sure) that you're trying to treat as an annotation.
烂柯人 2024-08-16 22:09:20

我终于想出了如何做到这一点。像往常一样,一旦您知道如何做,这并不难。只是想我会把这个传递下去。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
  {
    [super setSelected:selected animated:animated];
    NSLog(@"here I am in set selected");
    if (YES == selected)
    {
       NSLog(@"I am selected");
    }
    else 
   {
     self.backgroundColor = [UIColor clearColor];
      NSLog(@"not selected");
    }
  }

I finally figured out how to do this. As usual it's not that hard once you know how. Just thought I would pass this on.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
  {
    [super setSelected:selected animated:animated];
    NSLog(@"here I am in set selected");
    if (YES == selected)
    {
       NSLog(@"I am selected");
    }
    else 
   {
     self.backgroundColor = [UIColor clearColor];
      NSLog(@"not selected");
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文