我将calloutAccessoryControlTapped
中的值分配给双Lat
和Lon
,并在按钮操作中使用它,但我得到了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;
}
答案 0 :(得分:2)
不要实现 calloutAccessoryControlTapped
委托和附件按钮的自定义方法 - 执行其中一项操作。
如果同时执行这两项操作,则地图视图将同时调用两者,并且在您的情况下,在委托方法之前(以及设置Lat
和Lon
之前)调用自定义按钮方法。
建议仅使用calloutAccessoryControlTapped
委托方法。
infoButton:
的代码移至calloutAccessoryControlTapped
方法infoButton:
方法addTarget
viewForAnnotation
行
不推荐但是,如果您出于某种原因想要使用自定义infoButton:
而不是calloutAccessoryControlTapped
委托方法:
calloutAccessoryControlTapped
委托方法infoButton:
中,使用地图视图的selectedAnnotations
属性获取对当前所选注释的引用有关自定义方法选项的代码示例,请参阅How to recognize which pin was tapped。