我似乎无法在地图注释中添加公开按钮。
我也将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;
}
答案 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;
}