首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift:自定义CalloutView上的按钮动作

Swift:自定义CalloutView上的按钮动作
EN

Stack Overflow用户
提问于 2018-05-13 12:53:12
回答 1查看 80关注 0票数 1

我正在我的mapView上实现一个自定义mapView,并且我试图实现来自自定义calloutView的操作按钮。我很难找到检测与calloutViews相关的按钮的方法。

以下是我的执行情况:

代码语言:javascript
复制
//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或注释的帖子。是否有办法这样做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-13 13:17:27

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

代码语言:javascript
复制
calloutView.deleteButton.tag = <#setToIndexOfArr#>

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

//

代码语言:javascript
复制
@objc func deleteButtonTap(_ sender: UIButton) { 

  let item = modelArr[sender.tag]  

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50316328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档