0

I have a custom callout view in MapKit, which contains a button. I want to make it so that tapping this button will perform a given segue. How can I go about doing that since I am loading the custom callout from an .xib whose class is a subclass of UIView (and not UIViewController, so I can't just perform the segue in its class file)

EDIT

    if control == view.detailCalloutAccessoryView as? CustomCalloutView {
        if control.viewWithTag(6).touchesBegan {
        performSegueWithIdentifier("showRentalDetailsFromCalloutView", sender: self)
        }
    }
Jack Berstrem
  • 535
  • 1
  • 5
  • 22

1 Answers1

1

There is actually a method that detects a click on the callout. This is in Objective-C, but you can convert it to Swift and it will work for you:

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

Here is a reference to a more detailed answer about tapping on an MKAnnotation: MKMapView MKPointAnnotation tap event

Here is my rendition of this method in Swift. Disclaimer: I did not test this, but it should point you in the right direction.

func mapView(_ mapView: MKMapView,
    annotationView view: MKAnnotationView,
    calloutAccessoryControlTapped control: UIControl)
{

    performSegueWithIdentifier("SegueName", sender: self)

}

And just to be complete, here is a link to the MKMapViewDelegate documentation with the details on this function: MKMapViewDelegate Protocol Reference

Community
  • 1
  • 1
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • I actually did implement this... but I was hoping to make the click be specific to the button inside my callout, not to the entire callout view. – Jack Berstrem Feb 19 '16 at 20:31
  • You could add a tag or a name to the callout/mkannotationview and do some logic to decide which callout was clicked. – MSU_Bulldog Feb 19 '16 at 20:32
  • I tried writing logic that would identify the control but didn't get it to work. What kind of logic would you implement ? – Jack Berstrem Feb 19 '16 at 20:34
  • I just added the code I tried to run... it didn't work. Also, the basic implementation of calloutAccessoryControl didn't work. – Jack Berstrem Feb 19 '16 at 20:35
  • It really depends on how many annotations you are setting up, whether they are dynamic or fixed locations. But if you add a tag like `annotationView.tag = 1` then you can refer to that annotation's tag to figure out which annotation it is. Also, please add the entire function, where are you calling that code you just posted? – MSU_Bulldog Feb 19 '16 at 20:37
  • How do you think I could implement touchesBegan in the way I tried to do it earlier (see my edit) ? – Jack Berstrem Feb 19 '16 at 20:41
  • Honestly, don't do that. There is a `calloutAccessoryControlTapped` function for a reason, why not use it? – MSU_Bulldog Feb 19 '16 at 20:41
  • The touchesBegan implementation I am trying to do would be inside of calloutAccessoryControlTapped. – Jack Berstrem Feb 19 '16 at 20:42
  • I would detect the control, and then use that control to write an implementation of touchesBegan.... I honestly don't see a simpler way... – Jack Berstrem Feb 19 '16 at 20:46
  • The simpler way is to use the built in functionality to click on callouts.... It is really simple, just implement the `calloutAccessoryControlTapped` function, add a unique tag to each annotationView when you add it to your map, and refer to that tag to decide which segue you want to run. You are trying to make it complicated by building your own functionality, it is already there you just need to use it. – MSU_Bulldog Feb 19 '16 at 20:49
  • The segue I want to run is actually the same for all annotations. I'll give it a shot. – Jack Berstrem Feb 19 '16 at 20:54