Search code examples
iphonestoryboardmkannotationview

How to respond to annotation button press with Storyboard in iOS


I have the right button on an annotation as follows:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.tag = [annotation index];

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

customAnnotationView.rightCalloutAccessoryView = rightButton;

In my annotationViewClick method I would normally navigate to the detail view controller by using 'initWithNibName' and pushViewController

How do I do this when I am using storyboards? I guess I need a segue? Somebody give me a push in the right direction please.


Solution

    1. In storyboard, through Inspector window, you give the destination view controller an identifier (name), e.g. LocationDetailControllerIdentifier

    2. In the method that gets fired by tapping the callout accessory of annotation view:

    LocationDetailController *ldc = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationDetailControllerIdentifier"];
    // do further customization if needed
    ldc.title = @"Title";
    ...
    //assuming you are using UINavigationController
    [self.navigationController pushViewController:ldc animated:YES];
    

    -