How to generate an event in the MKPointAnnotation in IOS with Swift?

0

I hope the following example can explain me better to my question.

//Tengo mi mapa declarado
@IBOutlet weak var map: MKMapView!

//Defino mi locacion
let location = CLLocationCoordinate2DMake(latitud, longitud)

let span = MKCoordinateSpanMake(0.1, 0.1)
//genero un region en zoom    
let region = MKCoordinateRegion(center: location, span: span)

//declaramos una anotacion    
let annotation = MKPointAnnotation()
annotation.coordinate = location  
annotation.title = "King David Kalakaua"
annotation.subtitle = "Waikiki Gateway Park"

//Agrego la región al mapa     
map.setRegion(region, animated: true)

//Agrego la anotación al mapa
map.addAnnotation(annotation)

The following image shows the example of the previous code

My question is, if there is a way to generate the event click on the message window of the title and subtitle, for example, that it takes me to another ViewController and triggers a message. I really do not know if it's possible, and I'm just beginning to program in ios with swift and I'm a bit lost and in the official documentation I do not see it very clear, thanks:)

    
asked by Jesús Flor Farias 15.12.2016 в 22:06
source

1 answer

0

To get what you want you should assign the property delegate to your map:

map.delegate = self

Make your controller according to the protocol MKMapViewDelegate , and from there you can implement the delegated methods, which will make the click in that view recognize:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }
    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")

    annotationView.canShowCallout = true
    annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

    return annotationView
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    self.performSegue(withIdentifier: "openNextViewController", sender: self)
}
    
answered by 21.12.2016 / 17:26
source