2013-02-28 2 views
0

Я создатель noob для iphone developoment, и я пытаюсь определить значение int для аннотации, поэтому я могу взять это значение int и перекрестно ссылаться на массив, чтобы получить соответствующее значение. Однако, когда я пытаюсь получить значение int с помощью .tag-метода, значение всегда возвращается как ноль. Если у меня есть 5 аннотаций, мне нужно определить, какая из них - 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

Свойство тег является то, что вам нужно установить. Я думаю, вы нигде не устанавливаете его.

Конечно, для UIButton, который вы создаете с помощью buttonWithType. Значение тега по умолчанию равно 0, так что вы всегда будете получать 0, запрашивая тег сразу после создания представления (Button).

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