2014-02-07 68 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);

所以你可以看到什麼東西被設定在代碼運行的時間。

相關問題