Search code examples
iosnsstringmkannotationmkannotationview

Pass a string together an annotationView


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 */


    }

Solution

  • 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
         }