选定注释的问题

时间:2011-09-01 07:10:51

标签: iphone objective-c ios mkannotationview

:) 尝试检索所选注释的属性时,我有一个非常奇怪的问题。以下是我的问题的简短描述:

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

然后我将对象转换为Custom注释对象(因为这就是对象应该是什么)。

之后我尝试访问自定义注释临时对象的属性。一切都松了。对象的NSLog仅显示内存地址。 Text属性为null。所以基本上我无法访问它。

如果我做错了什么或者我应该使用什么方法,我将不胜感激。 谢谢你!

以下是代码:

-(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中发布文本。 我还有很长的路要走,直到我明白什么时候发布,什么时候不要一步一步!:)

1 个答案:

答案 0 :(得分:0)

这是您的release备忘单。

  

创建对象时,它包含newalloc中的任意一个,   在构造函数中copyretain然后你拥有它就可以了   如果您不再需要参考,请将其发布。