2013-02-28 72 views
0

我是iphone開發人員的noob,我試圖確定一個註釋的int值,所以我可以採取該int值並交叉引用它到一個數組來獲得相應的值。但是,當我嘗試使用.tag方法獲取int值時,該值始終返回爲零。如果我有5個annontations,我需要能夠確定哪個annontation是0,1,2,3和4.任何幫助,非常感謝。如何確定點擊哪個MKAnnotation?

我的代碼

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 

if (view.selected==YES) { 
    NSInteger annotationIndex = view.tag; //Always returns zero 
    NSLog(@"annotationIndex: %i", annotationIndex); 
    } 
} 

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
// Define your reuse identifier. 
    static NSString *identifier = @"MapPoint"; 

    if ([annotation isKindOfClass:[MapPoint class]]) { 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 
    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    annotationView.animatesDrop = YES; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    NSInteger clickIndex = rightButton.tag; //Always returns zero, despite there being 5 annotations 
    NSLog(@"buttonIndex: %i", clickIndex); 
    [rightButton addTarget:self 
        action:@selector(showDetails:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    annotationView.rightCalloutAccessoryView = rightButton; 

    return annotationView; 
    } 
    return nil;  
} 

回答

1

標籤屬性是必須設置的東西。我想你不會把它放在任何地方。

當然對於使用buttonWithType創建的UIButton。默認標籤值爲0,因此在創建視圖(按鈕)後,您總是會獲得0請求標籤。

0

下面寫代碼來獲得索引值

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
    // Annotation is your custom class that holds information about the annotation 
    if ([view.annotation isKindOfClass:[Annotation class]]) { 
    Annotation *annot = view.annotation; 
    NSInteger index = [self.arrayOfAnnotations indexOfObject:annot]; 
    } 
} 

與我以前的帖子https://stackoverflow.com/a/34742122/3840428

相關問題