Domanda

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  ];
È stato utile?

Soluzione

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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top