Swift,将sender中的参数传递给函数

Swift, passing parameters in sender to function

首先请不要将此标记为重复。我已经完成了所有其他问题和答案,并且已经改进了我最初拥有的代码,但我仍然无法弄清楚最后一部分。我的最后一年项目将于明天到期,我很乐意让这个功能发挥作用。谢谢!!

我有一个来自注释的自定义标注,当我单击它上面的按钮时,我想获得从用户位置到该注释的方向。

  let buttonDirections = UIButton(frame: calloutView.directions.frame)
      buttonDirections.addTarget(self, action: #selector(FindParkingVC.getDirections(sender:)), for: .touchUpInside)
      calloutView.addSubview(buttonDirections) 




  func getDirections(sender: UIButton) {

  //error on this line saying UIbutton has no member view
  //obviously the sender is wrong but don't know how to fix it  
  if let anno = sender.view.annotation as? SpaceAnnotation


 {

        var place: MKPlacemark!
        if #available(iOS 10.0, *) {
            place = MKPlacemark(coordinate: anno.coordinate)
        } else {
            place = MKPlacemark(coordinate: anno.coordinate, addressDictionary: nil)
        }
        let destination = MKMapItem(placemark: place)
        destination.name = "Selected Parking Space"
        let regionDistance: CLLocationDistance = 1000
        let regionSpan = MKCoordinateRegionMakeWithDistance(anno.coordinate, regionDistance, regionDistance)

        let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey:  NSValue(mkCoordinateSpan: regionSpan.span), MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] as [String : Any]

        MKMapItem.openMaps(with: [destination], launchOptions: options)
    }


}

现在,我以前在使用标准标注时都能正常工作。该功能像这样完美地工作。我现在没有使用 detailcalloutbutton,所以我不能使用这个功能。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if let anno = view.annotation as? SpaceAnnotation {

        var place: MKPlacemark!
        if #available(iOS 10.0, *) {
            place = MKPlacemark(coordinate: anno.coordinate)
        } else {
            place = MKPlacemark(coordinate: anno.coordinate, addressDictionary: nil)
        }
        let destination = MKMapItem(placemark: place)
        destination.name = "Selected Parking Space"
        let regionDistance: CLLocationDistance = 1000
        let regionSpan = MKCoordinateRegionMakeWithDistance(anno.coordinate, regionDistance, regionDistance)

        let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey:  NSValue(mkCoordinateSpan: regionSpan.span), MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] as [String : Any]

        MKMapItem.openMaps(with: [destination], launchOptions: options)
    }


}

编辑

这就是我调用 CustomCallout 视图的方式

  func mapView(_ mapView: MKMapView,
             didSelect view: MKAnnotationView)
{

    if view.annotation is MKUserLocation
    {
        // Don't proceed with custom callout
        return
    }

    let SpaceAnnotation = view.annotation as! SpaceAnnotation
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomCalloutView



    if SpaceAnnotation.temp3 != nil {

    calloutView.duration.text = "Available for " + SpaceAnnotation.temp3
    }

    if SpaceAnnotation.temp != nil {

    calloutView.price.text = "€" + SpaceAnnotation.temp
    }

    if SpaceAnnotation.temp2 != nil {
    calloutView.description_.text = "Description: "  + SpaceAnnotation.temp2
    }         


              let buttonDirections = UIButton(frame: calloutView.directions.frame)
      buttonDirections.addTarget(self, action: #selector(FindParkingVC.getDirections(sender:)), for: .touchUpInside)
      calloutView.addSubview(buttonDirections)



    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

另外可能值得注意的是 SpaceAnnotation 是一个 class,其中包含变量 temp3 等。因此在 init 中它会有更多以下内容

temp3 = snapshotValue["duration"] as?  String

UIButton 添加到 CustomCalloutView,然后添加到 MKAnnotationView。因此,MKAnnotationView是superview的父视图(向上两步):

if let anno = (sender.superview?.superview as! MKAnnotationView).annotation as? SpaceAnnotation {

}