我在我的应用程序中实现了以下功能
编译时没有错误。然而,我无法在模拟器中的地图注释上看到正确的附件按钮。我看到了注释,它只是没有按钮。 有没有一个技巧,我错过了?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id
<MKAnnotation>)annotation
{
static NSString *identifier = @"Vendor";
//Set upt the right accessory button
UIButton *myDetailAccessoryButton = [UIButton
buttonWithType:UIButtonTypeDetailDisclosure];
myDetailAccessoryButton.frame = CGRectMake(0, 0, 23, 23);
myDetailAccessoryButton.contentVerticalAlignment =
UIControlContentVerticalAlignmentCenter;
myDetailAccessoryButton.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter;
[myDetailAccessoryButton addTarget:self
action:nil
forControlEvents:UIControlEventTouchUpInside];
if ([annotation isKindOfClass:[Vendor class]])
{
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView
dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
annotationView.rightCalloutAccessoryView = myDetailAccessoryButton;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.calloutOffset = CGPointMake(-5, 5);
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
EDIT 这段代码有效,但上面的代码没有。
为什么?
if ([annotation isMemberOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:@"currentloc"];
UIButton *myDetailButton = [UIButton
buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment =
UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter;
[myDetailButton addTarget:self
action:nil
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = myDetailButton;
annView.animatesDrop=NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
答案 0 :(得分:1)
您确定要添加Vendor
类型的注释吗?
因为第一个代码块仅为rightCalloutAccessoryView
类型的注释设置Vendor
。对于任何其他类型,它返回nil
,导致通用标注(无按钮)。
第二个代码块为除rightCalloutAccessoryView
之外的任何注释类型设置MKUserLocation
。
顺便说一句,您已将按钮操作设置为nil
,这意味着点击时不会发生任何事情。