2014-06-09 3 views
0

Je travaille sur une application MapKit qui tombe punaises sur la carte sur certains endroits (code à la fin)qui calloutAccessoryControlTapped a été Indiquez appelé

Je le code a également ajouté la légende. Cependant, en fonction de la broche utilisée, j'essayais de charger des vues différentes et je n'ai trouvé aucun moyen de le faire bizarrement. J'ai placé un point d'arrêt et tout en progressant, j'ai remarqué qu'il y avait une ligne qui lisait. "[email protected]"myhome"; donc je me demandais si de toute façon je pouvais dire quoi appeler en fonction de quelle annotation était tapée.

Merci

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { 

    if([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    static NSString *identifier = @"myAnnotation"; 
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (!annotationView) 
    { 
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

     annotationView.pinColor = MKPinAnnotationColorRed; 
     annotationView.animatesDrop = YES; 
     annotationView.canShowCallout = YES; 
    }else { 
     annotationView.annotation = annotation; 
    } 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    return annotationView; 
} 
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

    NSLog(@"Callout Tapped"); 
} 

code --Updated

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { 

    if([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    // NSLog(annotation); 

    static NSString *identifier = @"myAnnotation"; 
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (!annotationView) 
    { 

     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

     annotationView.pinColor = MKPinAnnotationColorRed; 
     annotationView.animatesDrop = YES; 
     annotationView.canShowCallout = YES; 
    }else { 
     annotationView.annotation = annotation; 
    } 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    return annotationView; 
} 


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    id <MKAnnotation> annotation = [view annotation]; 
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) 
     { 
    // MKUserLocation *ann = (MKUserLocation *)view.annotation; 
    // NSLog(@"ann.title = %@", ann.title); 
     NSLog(@"HJ"); 
    } 
} 
+1

Dans 'calloutAccessoryControlTapped', l'annotation qui a été exploité est directement disponible à l'aide' view.annotation'. Par exemple: 'NSLog (@" Légende tapée, title =% @ ", view.annotation.title);'. Voir http://stackoverflow.com/questions/4565197/how-to-find-which-annotation-send-showdetails, http://stackoverflow.com/questions/22572342/how-to-access-to-an-annotation -attribute-inside-method-calloutaccessorycontrolta, etc. – Anna

+0

@Anna J'ai regardé le code. essayé de le mettre en œuvre. Je ne peux toujours pas le faire aussi. Je clique sur le bouton de révélateur et il ne lance pas l'instruction if comme si pour dire si ([annotation isKindOfClass: [classe MKPointAnnotation]]) n'évalue pas à vrai –

+0

@Anna Got it. Je n'ai pas remarqué que je mixais MKPointAnnotation avec MapAnnotation (Classe de la mienne) –

Répondre

0

Vous pouvez définir des balises pour dire 1 gauche et 2 pour une vue accessoire droit. Et dans votre méthode calloutAccessoryControlTapped, n'aiment:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

    if ([control tag] == 1) { 
     NSLog(@"tapped left accessory"); 
    } 
    else if ([control tag] == 2) { 
     NSLog(@"tapped right accessory"); 
    } 
} 
+0

J'attribuerais cette balise dans le code où j'ai mis l'étiquette d'annotation etc. –

+0

@SleepParalysis oui correct –

+0

ma MapAnnotation * myhome = [[MapAnnotation alloc] init]; ne semble pas avoir une méthode setTag –

Questions connexes