문제

I'm trying to perform a segue when a map annotation pin is tapped. Im using a custom annotation class if that makes a difference.

I've tried

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

    NSLog(@"annotation selected");

    [self performSegueWithIdentifier:@"mySegue" sender:self];
}

but my NSLog isnt running so I assume the method isnt getting called.

The only other things I've done are add the annotation pin to my map view and set my view controller as the map view delegate.

Here is how I added the annotation to the mapview

SPMapAnnotation *pin = [[SPMapAnnotation alloc] init];

pin.coordinate = spotLocation.coordinate;

pin.title = [spot objectForKey:@"spotName"];

[self.mapView addAnnotation:pin];

How can I make this work? That didSelectAnnotation method seems like it would make this easy to do but I'm not sure how it works.

도움이 되었습니까?

해결책

Apparently I forgot I didnt allocate my mapView until viewDidAppear. Just needed to add

self.mapView.delegate = self;

after I alloc and inited it.

다른 팁

Implement following delegate method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *annotationView = nil; 
    if ([annotation isKindOfClass:[SPMapAnnotation class]]) 
    { 
        annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; 
        if (annotationView == nil) 
        { 
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"]; 
            annotationView.canShowCallout = YES; 
            annotationView.animatesDrop = YES; 
        } 
    } 
    return annotationView; 
}

as didSelectAnnotationView will not be called for standard annotation pin, you need to return MKAnnotationView for didSelectAnnotationView to be called.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top