简体   繁体   中英

How do I make my annotation callout lead to a new View Controller?

As of now, I have a DetailViewController in my storyboard with no segues to/from. Here is my code in the map view controller...

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

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"User"];


UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = rightButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
UIImageView *myCustomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CorgiRunner2 Final.png"]];
myCustomImage.image = profPic;
myCustomImage.frame = CGRectMake(0,0,31,31);
MyPin.leftCalloutAccessoryView = myCustomImage;

[rightButton addTarget:self action:@selector(pinTouched:) forControlEvents:UIControlEventTouchUpInside];

return MyPin;
}




-(void)pinTouched:(UIButton *)sender
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

detailViewController.profName1 = profName;
detailViewController.profPic1 = profPic;
}

How do I make it so that when the call out is pressed, my Detail View Controller shows up?

Use calloutAccessoryControlTapped like this:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"tapped");
    DetailViewController *detailViewController = [[DetailViewController alloc]     initWithNibName:@"DetailViewController" bundle:nil];

    detailViewController.profName1 = profName;
    detailViewController.profPic1 = profPic;
}

Don't forget to set your mapView delegates and make sure this delegate method gets called.

Hope this helps

UPDATE I instead connected my mapView to my DetailView Controller in the main storyboard, and used

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"Details" sender:view];
}

THIS WORKS FOR ME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM