Frage

Ive got a few annotation on a map that is loaded. I know how to get the distance from the annotation to the users location and want to be able to show it in the subtitle when the user selects it.

I thought I could do it here.

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

But the problem is I can't assign a new subtitle because it complains that it is readonly.

Any thoughts?

War es hilfreich?

Lösung

According to your comment, the code you tried is something like this:

view.annotation.subtitle = @"something";

The compiler will give the error "Assignment to readonly property" here because the annotation property of MKAnnotationView is typed as the generic id <MKAnnotation>.

The MKAnnotation protocol defines subtitle as readonly as the default.


The MKPointAnnotation class (which you are using for your annotations according to another comment) implements MKAnnotation but overrides subtitle as write-able.


To avoid the compiler error, you need to cast view.annotation to MKPointAnnotation (which in your case it really is). For example:

if ([view.annotation isKindOfClass:[MKPointAnnotation class]])
{
    MKPointAnnotation *pa = (MKPointAnnotation *)view.annotation;
    pa.subtitle = @"something";
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top