0

我在我的应用程序中实现了以下功能

编译时没有错误。然而,我在模拟器中的地图注释上看不到正确的附件按钮。我看到注释,只是上面没有按钮。我错过了这个技巧吗?

- (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;
   }

编辑此代码有效,但上述代码无效。

这是为什么?

   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;
4

1 回答 1

1

您确定要添加 type 的注释Vendor吗?

因为第一个代码块只设置了rightCalloutAccessoryViewfor 类型的注解Vendor。对于任何其他类型,它返回nil导致通用标注(无按钮)。

第二个代码块rightCalloutAccessoryView为除 之外的任何注释类型设置MKUserLocation

顺便说一句,您已将按钮操作设置为nil这意味着当它被点击时不会发生任何事情。

于 2012-10-11T01:38:57.673 回答