1

我正在我的 mapView 上实现自定义 calloutView,并且我正在尝试从自定义 calloutView 实现操作按钮。我无法找到检测与 calloutViews 相关的按钮的方法。

下面是我的实现:

//At MapViewController
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if !annotation.isKind(of: Post.self) {
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView?.canShowCallout = true
        annotationView?.animatesDrop = true
    } else {
        annotationView?.annotation = annotation
    }

    let postAnnotation = annotation as! Post
    let calloutView = DetailCalloutView()
    calloutView.titleLabel.text = ...
    calloutView.timestampLabel.text = ...
    calloutView.postContentTextView.text = ...
    calloutView.profileImageView.image = ...
    calloutView.deleteButton.addTarget(self, action: #selector(deleteButtonTap), for: .touchUpInside)
    calloutView.chatButton.addTarget(self, action: #selector(chatButtonTap), for: .touchUpInside)

    annotationView?.detailCalloutAccessoryView = calloutView

    return annotationView
}

@objc func deleteButtonTap(_ sender: MKAnnotationView) {
    let post = sender.annotation as? Post
    let index = postArray.index(of: post) //Is there a way to implement something like this here??
    ...
}

@objc func chatButtonTap() {
    print("123")

}

//At Post
class Post: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var post: String?
    //All other properties

    init(coordinate: CLLocationCoordinate2D, post: String, ...) {
        self.coordinate = coordinate
        self.post = post
        ...
    }
}

//At DetailCallOutView
class DetailCalloutView: UIView {

    let containerView: UIView = {
        ...
        return v
    }()

    //All other UI elements

    let chatButton: UIButton = {
        let b = UIButton()
        ...
        return b
    }()

    let deleteButton: UIButton = {
        let b = UIButton()
        ...
        return b
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()
    }

    func setupViews() {
        addSubview(containerView)
        //adding subviews for all UI elements
        containerView.addSubview(cButton)
        containerView.addSubview(dButton)

        //setting up constraints for all UI elements    
    }
}

我能够检测到聊天和删除按钮,但无法获取关于 postArray 或注释的帖子。有没有办法这样做?

4

1 回答 1

0

第一个动作是目标添加到的类型,UIButton在这里

calloutView.deleteButton.tag = <#setToIndexOfArr#>

calloutView.deleteButton.addTarget(self, action: #selector(deleteButtonTap), for: .touchUpInside)

//

@objc func deleteButtonTap(_ sender: UIButton) { 

  let item = modelArr[sender.tag]  

}
于 2018-05-13T13:17:27.863 回答