在 MKAnnotationView 中点击按钮打开 NavigationController

Open NavigationController on tap of button in MKAnnotationView

我有 MKAnnotationView 显示标题、副标题和信息按钮的地方,点击位置图钉。

我添加了以下代码

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }


        let reuseId = "pin"
        let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        pinView.pinTintColor = UIColor.darkGrayColor()
        pinView.draggable = true
        let btn = UIButton(type: .DetailDisclosure)
        pinView.rightCalloutAccessoryView = btn

        let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:)))
        pinView.addGestureRecognizer(tapGesture)

        return pinView
    }

    func calloutTapped(sender: UITapGestureRecognizer) {
        //   if sender.state != UIGestureRecognizerState.Began { return }
        let annView: MKAnnotationView! = sender.view as? MKAnnotationView
        let ann:MKAnnotation! = annView!.annotation
        print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)")
        let touchLocation = sender.locationInView(mapView)
        let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView)
        print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)  " )

        let storyboard : UIStoryboard = UIStoryboard(name: "ShoppingCart", bundle: nil)
        let vc : ShoppingCartController = storyboard.instantiateViewControllerWithIdentifier("ShoppingCart") as! ShoppingCartController


        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)



    }

然而,在使用此代码后,点击地图上的图钉,用户将导航至 ShoppingCart 情节提要。我想在点击信息按钮时显示 ViewController 以及所点击事件的标题、副标题、纬度和经度。

这是可行的解决方案

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }


        let reuseId = "pin"
        let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        pinView.pinTintColor = UIColor.darkGrayColor()
        pinView.draggable = true
        let btn = UIButton(type: .DetailDisclosure)
        pinView.rightCalloutAccessoryView = btn

        let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:)))
        pinView.addGestureRecognizer(tapGesture)

        return pinView
    }

    func calloutTapped(sender: UITapGestureRecognizer) {


        //   if sender.state != UIGestureRecognizerState.Began { return }
        let annView: MKAnnotationView! = sender.view as? MKAnnotationView
        let ann:MKAnnotation! = annView!.annotation
        print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)")
        let touchLocation = sender.locationInView(mapView)
        let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView)
        print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)  " )
}