-1

The thing is that I have a main class : MyAnnotation, created to display annotations on my mapView.

@interface lieuAnnotation : MyAnnotation

@property(readonly, nonatomic) UIImage *uneImage; // I cannot access this property.

@end

I created a second class lieuAnnotation inheriting from this one with a new property (an UIImage).

@interface MyAnnotation : NSObject<MKAnnotation> {

    // Some variables
}

// Some methods

@end

On the map, when the pin is selected, I have set a disclosure indicator who calls the delegate method :

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
                      calloutAccessoryControlTapped:(UIControl *)control
{
    [self detailPinVue:view]; // Personnal method
}

Note that the disclosure indicator is only displayed for lieuAnnotation instances

So view.annotation should be a lieuAnnotation instance.

Then I want to access my property :

- (void)detailPinVue:(MKAnnotationView *)view
{
    [aView addSubview:view.annotation.uneImage];
}

The things is I can't access the property uneImage because Xcode tells me :

Property 'uneImage' not found on object of type 'id'

But in my mind, it should be possible !

So I also tried to access it with that way :

lieuAnnotation *anno = [[lieuAnnotation alloc] init];
anno = view.annotation;

[aView addSubview:anno.uneImage];

But it's not working…

Thanks for help and ideas.

Lucien
  • 451
  • 4
  • 16

3 Answers3

0

Try:

if ([view.annotation isKindOfClass:[lieuAnnotation class]]) { 
    lieuAnnotation *annotaion = (lieuAnnotation *)view.annotation;
    [aView addSubview:annotation.uneImage];
} else {
    NSLog(@"error %@ / %@", NSStringFromClass([view class]), NSStringFromClass([view.annotation class]));
}
Wain
  • 118,658
  • 15
  • 128
  • 151
0

Simple answer: You need to cast it before you access the property (but only do this if you are 100% sure the object in question has that property, otherwise you would get a EXC_BAD_ACCESS at runtime.

Explanation: the object in question seems to have the type id at compile time. id is a generic type for all objects in ObjC. Not all classes have an uneImage property, so the compiler can't tell if an id object has that property. What the compiler thinks: "let's play it safe and just don't build". Bottomline: you're smarter than the compiler (as you probably already now).

Fix:

- (void)detailPinVue:(MKAnnotationView *)view
{
    [aView addSubview: (lieuAnnotation *)view.annotation.uneImage];
}
11684
  • 7,356
  • 12
  • 48
  • 71
0

Check the way your annotation, by MKMapView addAnnotations:. Make sure that you're adding objects of your custom class.

And you can use NSLog(@"%@", view.annotation.class); to know the base class of the annotation.

BTW. The way you cast isn't necessary. lieuAnnotation *anno = (lieuAnnotation *)view.annotation; is the correct way.

Mohannad A. Hassan
  • 1,650
  • 1
  • 13
  • 24