所选注释存在问题

发布于 2024-12-02 16:30:25 字数 1962 浏览 2 评论 0原文

:) 当尝试检索所选注释的属性时,我遇到了一个非常奇怪的问题。这是我的问题的简短描述:

我将所选注释数组的第一个对象传递给一个新数组,因为它是我唯一需要的对象(根据Apple文档,将 selectedAnnotations 数组传递给新数组仅选择第一个对象但我确实尝试直接从索引路径 0 处的 selectedAnnotations 数组中提取对象,这是同样的问题)。

然后,我将该对象转换为自定义注释对象(因为这就是该对象应有的样子)。

之后,我尝试访问自定义注释临时对象的属性。这是一切都崩溃的时候。对象的 NSLog 仅显示内存地址。文本属性为空。所以基本上我无法访问它。

我将不胜感激任何有关我做错了什么或我应该使用什么方法的帮助。 谢谢您!

这是代码:

-(void) mapView:(MKMapView *)aMapView didSelectAnnotationView:(MKAnnotationView *)view
{

    if ([view isUserInteractionEnabled])
       // NSLog(@"Tapped!!!");

    {

     NSArray* selectedAnnotation=mapView.selectedAnnotations;
     CustomAnnotations *selectedAnn=[selectedAnnotation objectAtIndex:0];


       NSLog(@"selected annotation text is %@", selectedAnn.text);

我的自定义注释类有一个坐标和一个文本属性,并且使用此代码将其放置在地图上:

    CustomAnnotations* commentAnnotation = [[[CustomAnnotations alloc] initWithLocation:mapView.userLocation.location.coordinate andTitle:@"comment" andText:text]autorelease];


[mapView addAnnotation:commentAnnotation];

此外,注释视图具有以下编码:

-(MKAnnotationView *) mapView:(MKMapView *) aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *customAnnotation = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

    if(!customAnnotation)
    {
    customAnnotation = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]autorelease];
    }
    customAnnotation.centerOffset=CGPointMake(10, -30);


    if ([annotation title]==@"comment")  
    {
        customAnnotation.userInteractionEnabled=YES;

        customAnnotation.image=[UIImage imageNamed:@"NewCommentsPin.png"];
    }

    return customAnnotation;

}

任何帮助将不胜感激!

我发现了问题:我的自定义注释类正在释放 dealloc 中的文本。 我还有很长的路要走,直到我明白何时释放、何时不释放,但一步一步!:)

:)
I have a really strange problem when trying to retrieve the properties of a selected annotation. Here is the short description of my problem:

I pass the first object of the selected annotations array to a new array as it's the only one I need (acc. to Apple doc, passing the selectedAnnotations array to a new array only selects the first object. But I did tried to pull the object directly from the selectedAnnotations array at index path 0 and it's the same problem).

Then I transform the object into a Custom annotation object (as this is what the object should be).

Afterwards I try to access the properties of my custom annotation temp object. Here is when everything breaks loose. NSLog of the object only shows memory address. Text property is null. So basically I can't access it.

I would appreciate any help on what am I doing wrong or what approach should I use.
Thank you kindly!

Here is the code:

-(void) mapView:(MKMapView *)aMapView didSelectAnnotationView:(MKAnnotationView *)view
{

    if ([view isUserInteractionEnabled])
       // NSLog(@"Tapped!!!");

    {

     NSArray* selectedAnnotation=mapView.selectedAnnotations;
     CustomAnnotations *selectedAnn=[selectedAnnotation objectAtIndex:0];


       NSLog(@"selected annotation text is %@", selectedAnn.text);

My custom annotation class has a coordinate and a text property and it's being placed on map with this code:

    CustomAnnotations* commentAnnotation = [[[CustomAnnotations alloc] initWithLocation:mapView.userLocation.location.coordinate andTitle:@"comment" andText:text]autorelease];


[mapView addAnnotation:commentAnnotation];

Furthermore, the view for annotation has the following coding:

-(MKAnnotationView *) mapView:(MKMapView *) aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *customAnnotation = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

    if(!customAnnotation)
    {
    customAnnotation = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]autorelease];
    }
    customAnnotation.centerOffset=CGPointMake(10, -30);


    if ([annotation title]==@"comment")  
    {
        customAnnotation.userInteractionEnabled=YES;

        customAnnotation.image=[UIImage imageNamed:@"NewCommentsPin.png"];
    }

    return customAnnotation;

}

Any help would be much appreciated!

I figured out the problem: My custom annotation class was releasing the text in dealloc.
I still have a long way to go until I understand when to release and when not to but one step at a time!:)

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

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

发布评论

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

评论(1

最笨的告白 2024-12-09 16:30:25

这是您的发布备忘单。

当您创建一个对象时,它具有任何单词 newalloc
在构造函数中copyretain然后你拥有它并且你有
如果您不再需要该参考,请将其释放。

Here is your release cheat sheet.

When you create an object, and it has any of the words new, alloc,
copy or retain in the constructor then you own it and you have
to release it if you do not need the reference any more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文