Search code examples
iosswiftmapkitseguexcode-storyboard

Swift storyboard - Creating a segue in MapView using calloutaccessorycontroltapped disclosure button


In my MapView, I have several MKAnnotation pins that each have the viewFor configured to show the small popup with the disclosure "I" button on the right.

Now I am trying to find a way to create a segue from hitting the disclosure "I" button in the popup - I see that this can be done with the

func mapView(... calloutAccessoryControlTapped control: UIControl)

However, because in storyboard the MKAnnotation pins of course do not show up, I don't know how to use the control-drag method of creating the segue and getting the corresponding segue ID.

My workaround - I basically instantiate the other view controller that I wish to segue towards and switch to that new view whenever the "I" button is pressed

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    let settingVC = self.storyboard?.instantiateViewController(withIdentifier: "SetNotifPageID") as! SetNotificationViewController
    self.present(settingVC, animated: true, completion: nil)

}

But is there a better way to perform this, say from within storyboard, or maybe with a more correct approach?


Solution

  • To create the segue, in the storyboard, drag from the ViewController icon on one view controller to the view area of the second view controller.

    enter image description here

    Then call performSegue(identifier:sender) from mapView(_:annotationView:calloutAccessoryControlTapped:).

        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            performSegue(withIdentifier: "yourSegueIdFromTheStoryboard", sender: nil)
    }
    

    Configure your destination view controller from prepare(for:sender:).

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let viewController = segue.destination as? SetNotificationViewController {
                // configure your view controller
            }
        }