添加Disclosure Indicator以映射iOS 5的引脚

时间:2011-11-30 17:58:40

标签: ios ios5 mkmapview mapkit

我似乎无法在地图注释中添加公开按钮。

我也将MKMapViewDelegate实现到了我的视图控制器。我错过了什么?

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *mapPin = nil;
    if(annotation != map.userLocation) 
    {
        static NSString *defaultPinID = @"defaultPin";
        mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if (mapPin == nil )
            mapPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        mapPin.canShowCallout = YES;
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        mapPin.rightCalloutAccessoryView = infoButton;

    }
    return mapPin;
}

1 个答案:

答案 0 :(得分:3)

该代码应该有效,但请检查以下内容:

  • 是否设置了地图视图的delegate属性?声明视图控制器实现MKMapViewDelegate协议实际上并不告诉地图视图哪个对象正在实现委托方法。在代码中,执行map.delegate = self;或在IB中将delegate出口连接到文件所有者。
  • 注释的title属性是否设置为非空白?如果title为空,则不会显示标注。

此外,不相关但是,当dequeue返回注释视图时,您应该将其annotation属性更新为当前注释(它可能以前用于另一个注释)。此外,除非您使用ARC,否则还应autorelease视图。

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *mapPin = nil;
    if(annotation != map.userLocation) 
    {
        static NSString *defaultPinID = @"defaultPin";
        mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if (mapPin == nil )
        {
            mapPin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                         reuseIdentifier:defaultPinID] autorelease];
            mapPin.canShowCallout = YES;
            UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            mapPin.rightCalloutAccessoryView = infoButton;
        }
        else
            mapPin.annotation = annotation;

    }
    return mapPin;
}