在MKAnnotationView中点击按钮打开NavigationController

时间:2016-06-28 08:55:50

标签: ios swift mkannotationview

我有[dataLayer push:@{ @"event": @"openScreen", @"screenName": screenName }]; 我正在显示标题,副标题和信息按钮,点击位置图钉。

我添加了以下代码

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) } 故事板。我想在点击信息按钮上显示ViewController以及已点击的事件的标题,副标题,纬度和经度。

1 个答案:

答案 0 :(得分:0)

这是工作解决方案

    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)  " )
}