【问题标题】:Open NavigationController on tap of button in MKAnnotationView点击 MKAnnotationView 中的按钮打开 NavigationController
【发布时间】:2016-06-29 13:27:45
【问题描述】:

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

【问题讨论】:

    标签: ios swift mkannotationview


    【解决方案1】:

    这是可行的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2015-05-15
      • 2015-10-23
      • 2020-05-25
      相关资源
      最近更新 更多