Swift,将发送器中的参数传递给函数
首先,请不要将此标记为重复。我已经完成了所有其他问题和答案,并且已经改进了我第一次使用的代码,但是我仍然无法理解最后一部分。我有我的最后一年的项目,明天到期,将很乐意让这个功能的工作。谢谢 我有一个来自注释的自定义标注,当我单击该标注上的按钮时,我希望获得从用户位置到该注释的方向Swift,将发送器中的参数传递给函数,swift,swift3,sender,Swift,Swift3,Sender,首先,请不要将此标记为重复。我已经完成了所有其他问题和答案,并且已经改进了我第一次使用的代码,但是我仍然无法理解最后一部分。我有我的最后一年的项目,明天到期,将很乐意让这个功能的工作。谢谢 我有一个来自注释的自定义标注,当我单击该标注上的按钮时,我希望获得从用户位置到该注释的方向 let buttonDirections = UIButton(frame: calloutView.directions.frame) buttonDirections.addTarget(self,
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)
}
}
现在,我以前在使用标准标注时,这一切都正常工作。这个函数就这样完美地工作。我现在没有使用DetailCallout按钮,因此无法使用此功能
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是一个包含变量temp3等的类,因此在init中,它将包含以下更多内容
temp3 = snapshotValue["duration"] as? String
ui按钮
添加到customcallout视图
,然后将其添加到MKAnnotationView
。因此,MKAnnotationView
是superview的superview(向上两步):
if let anno = (sender.superview?.superview as! MKAnnotationView).annotation as? SpaceAnnotation {
}
ui按钮
添加到customcallout视图
,然后将其添加到MKAnnotationView
。因此,MKAnnotationView
是superview的superview(向上两步):
if let anno = (sender.superview?.superview as! MKAnnotationView).annotation as? SpaceAnnotation {
}
谢谢你的回复,对我来说很有意义。然而,当我运行它时,我现在得到了错误。。。。。无法将“EasyPark.CustomCalloutView”(0x1003be9e8)类型的值强制转换为“MKAnnotationView”(0x19ff3b578)?这是什么?这只是我项目的名称。CustomCalloutView是自定义详图索引。请添加CustomCalloutView标题代码。你如何使用它?我添加了一个编辑。不确定这是不是你要找的。谢谢你的时间谢谢你的回复,对我来说很有意义。然而,当我运行它时,我现在得到了错误。。。。。无法将“EasyPark.CustomCalloutView”(0x1003be9e8)类型的值强制转换为“MKAnnotationView”(0x19ff3b578)?这是什么?这只是我项目的名称。CustomCalloutView是自定义详图索引。请添加CustomCalloutView标题代码。你如何使用它?我添加了一个编辑。不确定这是不是你要找的。感谢您的时间在初始化callout视图然后使用它的位置添加代码。在初始化callout视图然后使用它的位置添加代码。