2016-06-28 73 views
1

我有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以及已被点击的事件的标题,副标题,纬度和经度。

回答

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