Swift - Perform Segue from map annotation

二次信任 提交于 2019-12-04 14:44:37

You need to implement viewForAnnotation method.

example.

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

        var rightButton: AnyObject! = UIButton.buttonWithType(UIButtonType.DetailDisclosure)
        rightButton.titleForState(UIControlState.Normal)

        pinView!.rightCalloutAccessoryView = rightButton as! UIView
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

If user tap (i) button on the annotation, calloutAccessoryControlTapped will be called.

ref. My MKMapView sample code https://github.com/koogawa/MKMapViewSample

EDIT

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        performSegueWithIdentifier("toTheMoon", sender: view)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toTheMoon" )
    {
        var ikinciEkran = segue.destinationViewController as! DetailViewController

        ikinciEkran.tekelName = (sender as! MKAnnotationView).annotation!.title

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!