Swift,将发送者中的参数传递给函数

标签 swift swift3 sender

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

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

  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 View 的方式

  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 是一个包含变量 temp3 等的类。因此在 init 中它会有更多以下内容

temp3 = snapshotValue["duration"] as?  String

最佳答案

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

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

}

关于Swift,将发送者中的参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42515430/

相关文章:

xcode - Swift 3使用 'withUnsafeBufferPointer'从网络套接字读取- 'init'无法使用 'withMemoryRebound'

ios - UIStackView 中的动态 View

swift - 无法在 TableCellView 中初始化 UIImage

swift - 如何比较两个字符串以检查它们是否具有相同的字符 Swift 4?

swift - 检查 NSUserDefaults 键是否存在

ios - 通用链接无法快速工作 3

ios - swift 3 中的 NSAttributedString 扩展

ios - UITapGestureRecognizer 操作中的引用发送者

email - 如何在 Jenkins 中设置发件人地址?

python - RTSP 视频流的发送方报告 (SR) 的绝对时间戳