开发者

Problem with selected annotations

开发者 https://www.devze.com 2023-04-01 21:16 出处:网络
:) 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 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 t开发者_如何转开发ext 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!:)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号