将一个字符串传递给annotationView

时间:2012-11-08 15:50:26

标签: ios nsstring mkannotation mkannotationview

在我的地图中有许多annotationView,每个都有自己的图像, annotationView的数据来自数据库中的不同表。 当我选择unannotationView时我想显示另一个viewController,为此我需要传递表名,这是一个字符串。我如何能? 我试过但是我收到了这个错误:

  No know instance method for selector 'setNameTable:'

这是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 

            }

          //.......


       }

在委托方法中,我必须传递字符串

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


    }

3 个答案:

答案 0 :(得分:0)

annotationView对象的annotation属性是MKAnnotation类,它对'nameTable'属性一无所知。

试试这个:

AnnotationCustom *annotationCustom= (AnnotationCustom *)annotation;
[annotationCustom setNameTable:[NSString stringWithFormat:self.name];

答案 1 :(得分:0)

尽管使用强制转换可以访问自定义属性和方法,但通常不应该在annotation委托方法中设置属性或调用viewForAnnotation对象上的方法。

由于您还尝试将属性设置为某个 class-instance-level 值(self.name),因此也无法保证其值与{{1}同步当前正在调用委托方法。

您应该做的是在创建注释时设置注释的属性,然后再调用annotationaddAnnotation

稍后,在委托方法(例如addAnnotations)中,您可以再次使用强制转换来检索自定义属性。

答案 2 :(得分:0)

我设法解决了这个问题。我在委托方法中向annotationView添加了一个强制转换:

  - (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

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

在另一个委托方法中:

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