【问题标题】:How to tell which button is being pressed in calloutAcessoryControlTapped?如何判断在 calloutAcessoryControlTapped 中按下了哪个按钮?
【发布时间】:2015-04-08 19:54:36
【问题描述】:

我正在使用 MapKit,并且我的别针中有 2 个标注配件。

我正在尝试实现一个用于更新图钉标题的按钮和一个用于删除图钉的按钮。

现在,每当我按下注释上的按钮时,它只会删除图钉。

如何让它对右键和左键做出不同的响应?

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    id <MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        NSLog(@"Clicked");
        if(view.rightCalloutAccessoryView){
             [self.mapView removeAnnotation:annotation];
        }
        else{
            float lat= annotation.coordinate.latitude;
            float longitude = annotation.coordinate.longitude;
            [self.mapView removeAnnotation:annotation];
            MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
            pointAnnotation.title = _titleOut.text;
            pointAnnotation.subtitle = _subtitle.text;
            pointAnnotation.coordinate = CLLocationCoordinate2DMake(lat, longitude);
            [self.mapView addAnnotation:pointAnnotation];
        }
    }
}

【问题讨论】:

    标签: ios mkmapview mkannotation mkannotationview


    【解决方案1】:

    这一行:

    if(view.rightCalloutAccessoryView){
    

    本质上说“如果 view.rightCalloutAccessoryView 不为零”。

    由于您在所有注释视图上设置右侧附件,if 条件将始终 em> 为真,因此点击 either 附件将执行 if 内的代码,即删除注释。

    相反,您想检查在调用委托方法的这种特定情况下点击了哪个按钮或控件(而不是视图是否定义了右侧附件)。

    幸运的是,委托方法准确地传递了在control 参数中点击的控件。

    control 参数可以直接与视图的右/左附件视图进行比较,以判断哪个被点击了:

    if (control == view.rightCalloutAccessoryView) {
    



    一些不相​​关的点:
    1. 注解中的latitudelongitude 属性属于CLLocationDegrees(又名double)类型,其精度高于float,因此为避免丢失精度,请使用CLLocationDegreesdouble

      CLLocationDegrees lat= annotation.coordinate.latitude;
      
    2. MKPointAnnotation 允许您直接更改title(它不像默认的id&lt;MKAnnotation&gt; 那样只读),因此您无需删除和创建新注释。它稍微简化了代码:

      -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
      
          if ([view.annotation isKindOfClass:[MKPointAnnotation class]])
          {
              NSLog(@"Clicked");
      
              if (control == view.rightCalloutAccessoryView) {
                  [self.mapView removeAnnotation:view.annotation];
              }
              else {
                  // Cast view.annotation as an MKPointAnnotation
                  // (which we know it is) so that compiler sees
                  // title is read-write instead of the
                  // default id<MKAnnotation> which is read-only.
                  MKPointAnnotation *pa = (MKPointAnnotation *)view.annotation;
      
                  pa.title = _titleOut.text;
                  pa.subtitle = _subtitle.text;
      
                  //If you want callout to be closed automatically after
                  //title is changed, uncomment the line below:
                  //[mapView deselectAnnotation:pa animated:YES];
              }
          }
      }
      

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 2014-03-14
      • 2010-10-18
      • 1970-01-01
      • 2011-02-10
      相关资源
      最近更新 更多