How to call a segue from a disclosure button on a map pin?

前端 未结 1 1386
梦如初夏
梦如初夏 2021-01-25 15:33

I have an app that plots pins on a mapview.

Each pin uses this code to display a detail disclosure button, which when tapped calls a showDetail method which then calls t

相关标签:
1条回答
  • 2021-01-25 16:28

    Usually you'd not add a target for the button, but rather just set the rightCalloutAccessoryView (like you have) and then write a calloutAccessoryControlTapped method, e.g.:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        static NSString *identifier = @"MyLocation";
        if ([annotation isKindOfClass:[MyLocation class]]) {
    
            MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
            if (annotationView == nil) {
                annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                annotationView.enabled = YES;
                annotationView.canShowCallout = YES;
                annotationView.image = [UIImage imageNamed:@"locale.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView
                annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            } else {
                annotationView.annotation = annotation;
            }
    
            return annotationView;
        }
    
        return nil;
    }
    
    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        if (![view.annotation isKindOfClass:[MyLocation class]])
            return;
    
        // use the annotation view as the sender
    
        [self performSegueWithIdentifier:@"DetailVC" sender:view];
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MKAnnotationView *)sender
    {
        if ([segue.identifier isEqualToString:@"DetailVC"]) 
        {
            DetailViewController *destinationViewController = segue.destinationViewController;
    
            // grab the annotation from the sender
    
            destinationViewController.receivedLocation = sender.annotation;
        } else {
            NSLog(@"PFS:something else");
        }
    }
    
    0 讨论(0)
提交回复
热议问题