简体   繁体   中英

Swift how to segue views by tapping a right accessory callout button on a map annotation

I am rather new to swift and object oriented programming and I am trying to switch views from a view that contains a map to another view by tapping on a right accessory callout button in the annotation of a pin that is dropped by a long press. In the storyboard, I have created a segue between the two views and assigned the segue with the identifier mapseg. Unfortunately, after trying everything I could find via google I cannot get the segue to occur when I tap the right accessory callout button and have no idea why.The application itself is a tabbed application with three tabs. The view for the second tab is the one that contains the map. Also, I don't know if this could have something to do with it, but the view I am trying to transition from is embedded in a navigation controller. Here is my code for the view that I am trying to transition from.

import UIKit
import MapKit

class SecondViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var MapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.MapView.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseID = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.enabled=true
        let btn = UIButton(type: .DetailDisclosure)
        btn.titleForState(UIControlState.Normal)
        pinView!.rightCalloutAccessoryView = btn as UIView
        return pinView
    }


    @IBAction func dropPin(sender: UILongPressGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.Began {
            let location = sender.locationInView(self.MapView)
            let locCoord = self.MapView.convertPoint(location, toCoordinateFromView: self.MapView)
            let annotation = MKPointAnnotation()

            annotation.coordinate = locCoord
            annotation.title = "City Name"
            annotation.subtitle = ""

            self.MapView.removeAnnotations(MapView.annotations)
            self.MapView.addAnnotation(annotation)
            self.MapView.selectAnnotation(annotation, animated: true)


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

            }
        }

    }

}

I think you need to override prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) method. And check whether if working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM