将值分配给双重问题

时间:2013-08-23 11:38:07

标签: ios mkmapview mkannotation mkannotationview

我将calloutAccessoryControlTapped中的值分配给双LatLon,并在按钮操作中使用它,但我得到了Lat和{{1}当我可以看到Lon中的值时,值为零。那么请问哪里可以成为我的问题?

H档案:

calloutAccessoryControlTapped

M档案:

@interface LocationViewController
  double Lat;
  double Lon;
}

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


Lat = [[view annotation]coordinate].latitude;
Lon = [[view annotation]coordinate].longitude;

NSLog(@"Lat: %f AND Lon %f", Lat, Lon); //The value is correct
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{

MKPinAnnotationView *pinAnnotation = nil;

if(annotation != locationMap.userLocation)
{
    static NSString *defaultPinID = @"myPin";

    pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinAnnotation.canShowCallout = YES;
    pinAnnotation.animatesDrop = YES;
    pinAnnotation.pinColor = MKPinAnnotationColorGreen;
    pinAnnotation.enabled = YES;

    //instatiate a detail-disclosure button and set it to appear on right side of annotation
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButton:) forControlEvents:UIControlEventTouchUpInside];
    pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}

1 个答案:

答案 0 :(得分:2)

不要实现 calloutAccessoryControlTapped委托附件按钮的自定义方法 - 执行其中一项操作。

如果同时执行这两项操作,则地图视图将同时调用两者,并且在您的情况下,在委托方法之前(以及设置LatLon之前)调用自定义按钮方法。

建议仅使用calloutAccessoryControlTapped委托方法。

  • 将当前位于infoButton:的代码移至calloutAccessoryControlTapped方法
  • 删除infoButton:方法
  • addTarget
  • 中删除viewForAnnotation


不推荐但是,如果您出于某种原因想要使用自定义infoButton:而不是calloutAccessoryControlTapped委托方法:

  • 删除calloutAccessoryControlTapped委托方法
  • infoButton:中,使用地图视图的selectedAnnotations属性获取对当前所选注释的引用

有关自定义方法选项的代码示例,请参阅How to recognize which pin was tapped