Ios 快捷注释单击按钮
我有一个带有自定义注释的地图视图。我还为此添加了一个按钮,但是否有办法查看按下按钮的注释 代码如下:Ios 快捷注释单击按钮,ios,swift,mkmapview,mkannotation,mkannotationview,Ios,Swift,Mkmapview,Mkannotation,Mkannotationview,我有一个带有自定义注释的地图视图。我还为此添加了一个按钮,但是否有办法查看按下按钮的注释 代码如下: func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if !(annotation is CustomPointAnnotation) { return nil } let reuseId = "tes
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.canShowCallout = true
anView!.enabled = true
anView?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
let imageview_left = UIImageView(frame: CGRectMake(0, 0, 20, 20))
imageview_left.image = UIImage(named: "left.png")
anView?.leftCalloutAccessoryView = imageview_left
let imageview_right = UIImageView(frame: CGRectMake(0, 0, 8, 13))
imageview_right.image = UIImage(named: "right.png")
anView!.rightCalloutAccessoryView = imageview_right
}
else {
anView!.annotation = annotation
}
let subtitleView = UILabel()
subtitleView.font = UIFont(name: "GillSans-Light", size: 14)
subtitleView.numberOfLines = 1
subtitleView.text = annotation.subtitle!
anView!.detailCalloutAccessoryView = subtitleView
let cpa = annotation as! CustomPointAnnotation
anView!.image = UIImage(named:cpa.imageName)
return anView
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView {
print("Pressed!")
}
print("Click")
}
当我点击右边的Callout AccessoryView时,什么都没有发生。当我点击标题或副标题注释时,什么也没有发生。我做错了什么?根据苹果文档,按下
leftCalloutAccessoryView
或rightCalloutAccessoryView
的委托方法只有在它们是UIControl
的后代时才会被调用
因此,您可以这样做,也可以自己处理手势,例如将uitappesturerecognizer
的实例添加到相应的视图中。
请记住,如果这些是UIImageView
的实例,您还必须设置instance.userInteractionEnabled=true
方法是将目标添加到按钮:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// ...
if anView == nil {
// ...
let button = MyButton(type: .DetailDisclosure)
button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)
anView!.rightCalloutAccessoryView = button
}
return anView
}
func showAnnotationDisclosure(sender: AnyObject) {
print("Disclosure button clicked")
}
现在,如果您想知道单击了什么注释,您必须为UIButton
创建子类:
class MyButton : UIButton {
var annotation: CustomPointAnnotation? = nil
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// ...
if anView == nil {
// ...
let button = MyButton(type: .DetailDisclosure)
button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)
anView!.rightCalloutAccessoryView = button
}
if let button = anView!.rightCalloutAccessoryView as? MyButton {
button.annotation = annotation
}
return anView
}
func showAnnotationDisclosure(sender: MyButton) {
print("Disclosure button clicked")
print(sender.annotation?.title)
}
它正在工作!谢谢,但是我有一个错误:button.annotation=注释错误:无法将类型“MKAnnotation”的值分配给类型“CustomPointAnnotation”,当然我不知道单击了什么注释。将其转换为CustomPointAnnotation,并使用as!非常感谢。button.annotation=注释为!CustomPointAnnotation