【问题标题】:How do I put a right accessory button onto the current location callout?如何将正确的附件按钮放在当前位置标注上?
【发布时间】:2012-02-03 16:52:17
【问题描述】:

我想在当前用户位置标注中添加一个右附件按钮。我发现了一些关于该主题的问题,但它们似乎对我不起作用。这是我到目前为止的代码,我已经成功地将正确的附件按钮放在我的其他注释上:

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
        //TODO: this doesn't seem to be working?  
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        MKAnnotationView * aV = [mapView viewForAnnotation:annotation];
        aV.canShowCallout = YES;
        aV.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        return nil;
    }
    return nil;
}

我在几个地方读到解决方案是为用户位置制作自定义 MKAnnotationView,但我还没有看到如何为用户位置完成此操作的演示——我已经成功地为我地图上的其他注释。

示例代码将是最有帮助的,谢谢!

【问题讨论】:

    标签: ios gps mkmapview mkannotation mkannotationview


    【解决方案1】:

    您应该再次获得用户位置注释视图的参考。 在 iOS >= 5 中调用以下方法

    选项 1

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        MKAnnotationView *aV; 
         for (aV in views) {
               if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
                    MKAnnotationView* annotationView = aV;
                    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                }
         }
    }
    

    请参阅下面的委托方法

    替代方法

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
         MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];   
         annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    

    确保添加委托方法,以便在点击标注按钮时执行任何操作:

    -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    

    在任何 iOS 中,这些方法中的任何一个都会被调用,您可以向其添加右/左附件按钮。通过检查用户位置注释视图的子视图计数,确保您不会两次添加此按钮。

    您也可以在运行时添加/删除附件视图,但我相信您不会想删除它。按钮

    【讨论】:

    • 谢谢@chatur,确实有效!顺便说一句,您如何同时支持 iOS 4.x 和 iOS 5 版本?
    【解决方案2】:

    你的主要问题是你在这两种情况下都返回 nil 。您还应该使用重用标识符。

    - (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
     {
    
     if ([annotation isKindOfClass:[MKUserLocation class]]) {
        MKAnnotationView * aV = [mapView dequeueReusableAnnotationViewWithIdentifier:@"locationView"];
        if (aV == nil) 
           aV = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"locationView"];
        aV.canShowCallout = YES;
        aV.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        return aV;
      }
    return nil;
    }
    

    您还需要实现:

    - (void)mapView:(MKMapView *)_mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    

    用于标注附件的行为。

    【讨论】:

    • 看起来很有希望,我会试一试并相应投票!
    • 这对你有用吗?我的一个应用程序中的代码几乎相同,我没有遇到任何问题。
    • 不怕!我现在在地图上看不到用户位置标记,有些不对劲。如果我将它改回返回 nil,那么它会像以前一样工作,但当然附件按钮不会显示。有什么建议吗?
    • 这不起作用,因为MKAnnotationView 本身不显示任何内容。您需要设置图像属性或使用子类。蓝色的用户位置视图由一个名为MKModernUserLocationView(在iOS 7 中)的私有子类实现。 @chatur 的回答是正确的。
    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2019-05-09
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多