2013-02-28 78 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

相关问题