为什么还要在注释视图上调用calloutAccessoryControlTapped? [英] Why calloutAccessoryControlTapped is called for tap on annotation view also?

查看:171
本文介绍了为什么还要在注释视图上调用calloutAccessoryControlTapped?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器上有一个地图,我不知道为什么,但是当我仅点击注释视图时,不仅调用细节闭合时,也会调用委托calloutAccessoryControlTapped .那么为什么会这样呢?

I have a map on my view controller and I don't know why but the delegate calloutAccessoryControlTapped is also called when I just tap on annotation view, not only when I tap on detail closure. So why this behavior?

import UIKit
import MapKit

extension MapVC: MKMapViewDelegate, CLLocationManagerDelegate
{    
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
    {
        ...
    }

}

推荐答案

Massimo Polimeni 似乎是正确的. rightCalloutAccessoryView似乎有问题,而leftCalloutAccessoryView则没有问题.

Massimo Polimeni seems to be correct. There appears to be a problem with the rightCalloutAccessoryView, but not with the leftCalloutAccessoryView.

下面的代码(带有leftCalloutAccessoryView)可以正常工作.如果您点击左侧附件,它将打印已选择左侧附件".如果您点击标注标题,它将不会打印任何内容.

The code below (with a leftCalloutAccessoryView) works as expected. If you tap the left accessory, it prints "left accessory selected". If you tap the callout title, it doesn't print anything.

如果您使用rightCalloutAccessoryView(在下面注释掉)并点击右边的附件,它将打印选择了右边的附件". 如果您点击标注标题,它还会打印选择了正确的附件" .这似乎不正确.

If you use a rightCalloutAccessoryView (commented out, below) and tap the right accessory, it prints "right accessory selected". If you tap the callout title, it also prints "right accessory selected". This does not seem correct.

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: 50.29, longitude: -107.79)
        annotation.title = "Swift Current"

        mapView.addAnnotation(annotation)
        mapView.mapType = .standard
        mapView.delegate = self
        mapView.region = MKCoordinateRegion(
            center: CLLocationCoordinate2D(latitude: annotation.coordinate.latitude,
                                           longitude: annotation.coordinate.longitude),
            span: MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)
        )
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseId = "Annotation"
        var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            view?.canShowCallout = true
            view?.leftCalloutAccessoryView = UIButton(type: .detailDisclosure)
            //view?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        } else {
            view?.annotation = annotation
        }
        return view
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.leftCalloutAccessoryView {
            print("left accessory selected")
        } else if control == view.rightCalloutAccessoryView {
            print("right accessory selected")
        }
    }
}

这篇关于为什么还要在注释视图上调用calloutAccessoryControlTapped?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆