2016-06-28 72 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) ") 
}