0
我想在目标c中设置DetailDisclosure
按钮中的标签。我的旧代码是:在详细信息中设置标签
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
else
{
static NSString * const identifier = @"MyCustomAnnotation";
static int i;
MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
NSLog(@"%i",annotationView.tag);
annotationView.tag=annotationView.tag+1;
NSLog(@"%i",annotationView.tag);
annotationView.image = [UIImage imageNamed:@"pin1.png"];
annotationView.canShowCallout = YES;
UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newlocation.png"]];
annotationView.leftCalloutAccessoryView = sfIconView;
UIButton *InformationButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[InformationButton addTarget:self action:@selector(annotationButtClicked:) forControlEvents:UIControlEventTouchUpInside];
InformationButton.tag=i;
annotationView.rightCalloutAccessoryView=InformationButton;
UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPressEvent:)];
[annotationView addGestureRecognizer:longGesture];
return annotationView;
}
}
功能实现
- (void)annotationButtClicked:(id)sender
{
NSLog(@"%i",[sender tag]);
}
控制台输出为每次0.1
如何设置标签的DetailDisclosure
?
你可以修改你的代码到InformationButton.tag = 10;侮辱InformationButton.tag = i;然后再试一次。我相信我是0,这就是为什么你的日志返回0. – Greg
注意名称'InformationButton'以小写字母开头。 – Larme
请不要使用标签!有更好的内置方法。见http://stackoverflow.com/questions/9876042/annotation-details-after-detail-disclosure-pressed和http://stackoverflow.com/questions/9462699/how-to-recognize-which-pin-was-tapped 。 – Anna