gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:16:15 24 4
gpt4 key购买 nike

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

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

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com