【问题标题】:MKPinAnnotationView versus MKAnnotationViewMKPinAnnotationView 与 MKAnnotationView
【发布时间】:2012-05-16 23:21:00
【问题描述】:

我继承了一个引发此警告的项目

Incompatible pointer types assigning to 'MKPinAnnotationView *' from 'MKAnnotationView *'

在这一行

pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
    }

我想在没有警告的情况下退回项目,所以我希望这里有人能快速回答

完整代码:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 

    NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];

    if(annotation != mapView.userLocation) 

    {

        static NSString *defaultPinID = @"com.invasivecode.pin";

        pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:defaultPinID];


        if (!pinView) {
            pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
        }

    }   
    pinView.animatesDrop=YES;
    [mapView.userLocation setTitle:@"I am here"];
    [mapView.userLocation setSubtitle:[prefs objectForKey:@"CurrentLocationName"]];
    return pinView;        
}

谢谢!

【问题讨论】:

    标签: iphone mkmapview mkannotationview mkpinannotationview


    【解决方案1】:

    您正在对 pin 注释视图进行出队,同时您将注释视图分配为您的 pinview,这在技术上是错误的!!!这就是我猜它发出警告的原因。试试这个可能会解决您的问题。

    -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {
    
    
    pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    
    
    if (!pinView) {
            pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
        }
    
    ..........
    ..........
    
    }
    

    【讨论】:

      【解决方案2】:

      pinView 变量被声明为MKPinAnnotationView,但该行创建了一个MKAnnotationView

      改变这一行:

      pinView=[[[MKAnnotationView alloc]initWithAnnotation...
      

      到:

      pinView=[[[MKPinAnnotationView alloc]initWithAnnotation...
      


      您还应该有一个 else 部分到该 if 来处理注释视图重用:

      else
          pinView.annotation = annotation;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        • 2015-05-25
        • 2012-11-07
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        相关资源
        最近更新 更多