2014-02-07 69 views
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

+0

你可以修改你的代码到InformationButton.tag = 10;侮辱InformationButton.tag = i;然后再试一次。我相信我是0,这就是为什么你的日志返回0. – Greg

+0

注意名称'InformationButton'以小写字母开头。 – Larme

+0

请不要使用标签!有更好的内置方法。见http://stackoverflow.com/questions/9876042/annotation-details-after-detail-disclosure-pressed和http://stackoverflow.com/questions/9462699/how-to-recognize-which-pin-was-tapped 。 – Anna

回答

1

如上所述,您不应该用大写字母命名变量,这在Objective c中通常表示一个类。

尝试使用此语法来设置标签的值。

[InformationButton setTag:i]; 

也可以尝试NSLog(@"tag to set = %d", i);

所以你可以看到什么东西被设定在代码运行的时间。

相关问题