Search code examples
iosmapkitmkannotation

[MKAnnotation]warning: '-setTitle:' not found in protocol(s)


i have a subclass named MyAnnotation to manage the annotation protocol, my problem is when i build the application, it gives me this warning :

'-setTitle:' not found in protocol(s)   

the compiler point me to this line which causes the warning :

annView.annotation.title = @"You are here";

where annView is a MKPinAnnotationView :

MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentLoc"];

can you please help me to fix that warning ? thx in advance :)


Solution

  • The annotation property of MKPinAnnotationView (which is a subclass of MKAnnotationView) is of type id<MKAnnotation> which doesn't define a -setTitle: method.

    However, if your class that implements the MKAnnotation protocol defines a -setTitle: method for itself (using an @property declaration, for example), then you can set the title as follows:

    MyAnnotationClass *myAnnot = (MyAnnotationClass *)annView.annotation;
    myAnnot.title = @"You are here";
    

    But you should probably set the annotation's title when you create it and before calling addAnnotation instead of setting it in viewForAnnotation.

    Also, naming your class MkAnnotation is probably not a good idea. It can easily be confused with the MKAnnotation protocol. Use something like MalekAnnotation instead.