Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

ios - How to access to an annotation attribute inside method calloutAccessoryControlTapped

I am trying to open a detail viewController when a user taps on the callout from a map annotation.

I have created a custom annotation subclass called myAnnotation, and there I have included a property called idEmpresa.

At a custom method I am declaring the annotation as follows:

double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

CLLocationCoordinate2D lugar;
lugar.latitude = latitud;
lugar.longitude = longitud;

NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];              

CLLocationCoordinate2D coordinate3;
coordinate3.latitude = latitud;
coordinate3.longitude = longitud;
myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre ];

annotation3.grupo = 1;

int number = [[[categorias objectAtIndex:i] objectForKey:@"idObjeto"] intValue];

annotation3.idEmpresa = number;
NSLog(@"ESTA ES LA ID DE LA EMPRESA %d",number);
[self.mapView addAnnotation:annotation3];

You may see that the annotation has an attribute annotation3.idEmpresa.

Then, at method calloutAccessoryControlTapped I need to have access to this attribute. I know how to access the annotation title and subtitle inside that method:

NSString *addTitle = [[view annotation] title  ];
NSString *addSubtitle = [[view annotation] subtitle  ];

But it doesn't work for the attribute idEmpresa

NSString *addTitle = [[view annotation] title  ];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The annotation property in MKAnnotationView is typed generically as id<MKAnnotation>.

That means it will point to some object that implements the MKAnnotation protocol.
That protocol only defines three standard properties (title, subtitle, and coordinate).

Your custom class myAnnotation implements the MKAnnotation protocol and so has the three standard properties but also some custom ones.

Because the annotation property is typed generically, the compiler only knows about the three standard properties and gives warnings or errors if you try to access custom properties that are not in the standard protocol.

To let the compiler know that the annotation object in this case is specifically an instance of myAnnotation, you need to cast it so that it will let you access the custom properties without warnings or errors (and the code completion will also start helping you).

Before casting, it's important to check that the object really is of the type you want to cast it to using isKindOfClass:. If you cast an object to a type that it really isn't, you'll most likely end up generating exceptions at run-time.

Example:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{   
    if ([view.annotation isKindOfClass:[myAnnotation class]])
    {
        myAnnotation *ann = (myAnnotation *)view.annotation;
        NSLog(@"ann.title = %@, ann.idEmpresa = %d", ann.title, ann.idEmpresa);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...