Jas*_*Lee 4 iphone cocoa cocoa-touch mkmapview ios
我在MKMapView上使用MKPinAnnotationView,标题和副标题正常显示,但未rightCalloutAccessoryView
显示.
我试了很多谷歌,但还没有显示出来.我的代码如下.
- (void)addAnnotation
{
CLLocationCoordinate2D theCoordinate = self.location.coordinate;
MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:theCoordinate];
annotation.title = @"Pin";
annotation.subtitle = @"More information";
[self.mapView addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MapAnnotation class]]) {
static NSString *reuseIdentifier = @"MyMKPinAnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
annotationView.pinColor = MKPinAnnotationColorRed;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
} else {
annotationView.annotation = annotation;
}
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
请注意,可以显示标题和副标题.
提前致谢.
查看标题和副标题是注释视图的默认行为.如果你想做任何其他的事情(例如rightCalloutAccessorView
),你必须确保viewForAnnotation
通过设置你的方式来调用delegate
你MKMapView
,并确保你viewForAnnotation
正在返回annotationView
你正在创建的.
发生了两个问题之一:
在所有情况下你viewForAnnotation
都会回来nil
.确保annotationView
在创建/修改后返回.
如果delegate
尚未设置地图视图,则viewForAnnotation
根本不会调用当前视图.
确保你已经设置好了delegate
.您可以以编程方式执行此操作:
self.mapView.delegate = self;
Run Code Online (Sandbox Code Playgroud)
或者您可以在IB中执行此操作(通过选择连接检查器,右侧面板的最右侧选项卡和control-drag从delegate
场景的状态栏):
如果是这种情况,如果你NSLog
在你的中添加一个语句或断点viewForAnnotation
.如果delegate
没有正确设置,您可能根本看不到它被调用.