UIButton in detailCalloutAccessoryView触摸时不会触发

时间:2017-04-17 20:29:02

标签: ios uibutton mkmapview mapkit

我已经在相当长的一段时间内寻找过这方面的帮助,我希望你们能帮忙..请在编程结构中放下我的菜鸟技能: - )

我已经定制了我的detailCalloutAccessoryView,我在这里循环一个数组,它将生成一个按钮,该按钮将调用方法

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationViewToShow: MKAnnotationView?

    if let title = annotation.title, title == "Din valgte position" {
        return nil
    }
    if (annotation is MKUserLocation) {
        return nil
    }
    let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    let myView = UIView()

    var strSubtitleStringToReplace = ""
    var strSubtitleStringToReplaceWith = ""
    var uibuttonYCoordinate = 30
    //------------------------------------
    let phoneArray = String(describing: annotation.subtitle).components(separatedBy: "\\r")
    var counter = 0
    for word in phoneArray{
        counter += 1
        if counter > 2 {
            let phoneNumber = word.replacingOccurrences(of: "\"))", with: "")
            strSubtitleStringToReplace += "\r" + phoneNumber
            strSubtitleStringToReplaceWith += "\r"
            let button2 = UIButton(frame: CGRect(x: -10, y: uibuttonYCoordinate , width: 100, height: 50))
            button2.setTitle(phoneNumber, for: .normal)
            button2.isExclusiveTouch = true
            button2.backgroundColor = .red
            button2.sizeToFit()

            let attributes2 = [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]
            let attributedText2 = NSAttributedString(string: button2.currentTitle!, attributes: attributes2)

            button2.setAttributedTitle(attributedText2, for: .normal)
            button2.sizeToFit()
            button2.setTitleColor(.black, for: .normal)
            button2.titleLabel?.font = UIFont(name: "Gill Sans", size: 16.0)
            button2.addTarget(self, action: #selector(self.makeCall), for: .touchUpInside)
            myView.addSubview(button2)
            uibuttonYCoordinate += 20


        }
    }

} 然后我将uiview添加到annotationview

        av.detailCalloutAccessoryView = myView
    av.canShowCallout = true

并且一切都显示正确..这将说明按钮显示正确的信息..

问题是当我点击注释并显示详细视图然后触摸/单击按钮时,MakeCall未被调用..就像它只注册详细视图上的触摸一样..有趣的是,如果我点击地图上的任何地方并且未显示详细视图,然后我再次触摸/点击注释,那么该按钮将按原样运行..

有谁知道什么是错的?

0 个答案:

没有答案