我想在目标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。
如何在DetailDisclosure
中设置标记?
答案 0 :(得分:1)
如上所述,你不应该用大写字母命名你的变量,这在Objective c中通常表示一个Class。
请尝试使用此语法来设置标记的值。
[InformationButton setTag:i];
同时尝试NSLog(@"tag to set = %d", i);
因此,您可以看到代码运行时所设置的内容。