gpt4 book ai didi

Swift:自定义 CalloutView 上的按钮操作

转载 作者:行者123 更新时间:2023-11-30 11:30:26 25 4
gpt4 key购买 nike

我正在我的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 或注释的帖子。有没有办法做到这一点?

最佳答案

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

calloutView.deleteButton.tag = <#setToIndexOfArr#>

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

//

@objc func deleteButtonTap(_ sender: UIButton) { 

let item = modelArr[sender.tag]

}

关于Swift:自定义 CalloutView 上的按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com