0

In my map there are many annotationView, each with their own image, the data of annotationView are taken from different tables in a database. When I select un'annotationView I want to display another viewController and to do this I need to pass the table name, which is a string. How do I? I tried but I get this error:

  No know instance method for selector 'setNameTable:'

This is the code in MapViewController.m

  - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
   <MKAnnotation>)annotation
  {
      //....

      if ([annotation isKindOfClass:[AnnotationCustom class]]){

       static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

      MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
                   dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];


       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:AnnotationIdentifier];  

      if ([self.name isEqualToString:@"MyTable"]){

            if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
                annotationView.image = [UIImage imageNamed:@"iphone_myTable.png"];

               [annotationView.annotation setNameTable:[NSString 
                   stringWithFormat:self.name]]; //the error is here
               //nameTable is a property of AnnotationCustom class 


            }else{
                annotationView.image = [UIImage imageNamed:@"ipad_myTable.png"];

                [annotationView.annotation setNameTable:[NSString 
                   stringWithFormat:self.name]];//the error is here
                 //nameTable is a property of AnnotationCustom class 

            }

          //.......


       }

In the delegate method I must pass the string

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control{

      AnnotationCustom *customAnnotation = (AnnotationCustom *)view.annotation;
     [customAnnotation setNameTable:[NSString stringWithFormat:@"%@",self.name]];

     NSLog(@"The name table is  %@",customAnnotation.nameTable); 
     /*the name is wrong, the string self.name is relative to last table open and not the 
      table  selected */


    }
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70

3 Answers3

0

The annotation property of the annotationView object is of class MKAnnotation which doesn't know anything about a 'nameTable' property..

Try this:

AnnotationCustom *annotationCustom= (AnnotationCustom *)annotation;
[annotationCustom setNameTable:[NSString stringWithFormat:self.name];
NikosM
  • 1,131
  • 7
  • 9
  • Is no longer the same error, but I can not pass the string when selecting the annotationView – Ortensia C. Nov 08 '12 at 16:02
  • Have a look at the answer above from Anna Karenina. The annotations' properties should be set at the point of their creation. – NikosM Nov 08 '12 at 16:06
0

Although casting will give you access to custom properties and methods, you generally should not be setting properties or calling methods on the annotation object in the viewForAnnotation delegate method.

Since you are also trying to set the property to some class-instance-level value (self.name), there's also no guarantee that its value is in sync with the annotation the delegate method is currently being called for.

What you should do is set the annotation's property when you create the annotation and before calling addAnnotation or addAnnotations.

Later, in the delegate methods (such as calloutAccessoryControlTapped), you can again retrieve the custom properties using casting.

  • I tried to retrieve the custom property using casting, but out the name wrong. The string is relative to last table open and not the table selected. I added the code to show – Ortensia C. Nov 09 '12 at 09:06
  • I set the property of customAnnotation when I created it, in viewDidLoad, but in delegate methods is not visible – Ortensia C. Nov 09 '12 at 09:13
  • In the delegate methods, do not call setNameTable. Just read the property. Show the code where you create the annotation. –  Nov 09 '12 at 11:47
0

I managed to solve the problem. I added a cast to annotationView in the delegate method:

  - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
  <MKAnnotation>)annotation
  {
          //....
        static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

        MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
               dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];


       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                              reuseIdentifier:AnnotationIdentifier];

       //I add in this position the cast
      AnnotationCustom *customAnnotation = (AnnotationCustom *)annotationView.annotation;

      if ([self.name isEqualToString:@"myTable"]){

            if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
                annotationView.image = [UIImage imageNamed:@"iphone_myTable.png"];
                customAnnotation.nameTable = self.name; //I assign this the property


           }else{
                annotationView.image = [UIImage imageNamed:@"ipad_myTable.png"];
                customAnnotation.nameTable = self.name; //I assign this the property

            }
      }
           //.......
   }

In another delegate method:

   - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
        calloutAccessoryControlTapped:(UIControl *)control{

        AnnotationCustom *customAnnotation = (AnnotationCustom *)view.annotation;

        NSLog(@"The name Table is %@",customAnnotation.nameTable);      
        //the result is correct
     }
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70