0

在我的地图中有很多 annotationView,每个都有自己的图像,annotationView 的数据取自数据库中的不同表。当我选择 un'annotationView 时,我想显示另一个 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 */


    }
4

3 回答 3

0

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

由于您还尝试将属性设置为某个类实例级别的值 ( self.name),因此也不能保证其值与annotation当前调用的委托方法同步。

您应该做的是在创建注释时和调用or之前设置注释的属性addAnnotationaddAnnotations

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

于 2012-11-08T16:03:18.750 回答
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
     }
于 2012-11-09T12:21:11.020 回答
0

annotationView 对象的 annotation 属性属于 MKAnnotation 类,它对“nameTable”属性一无所知。

尝试这个:

AnnotationCustom *annotationCustom= (AnnotationCustom *)annotation;
[annotationCustom setNameTable:[NSString stringWithFormat:self.name];
于 2012-11-08T15:54:56.443 回答