title: Swift: Button Action on Custom CalloutView
body: <p>I am implementing a custom calloutView on my mapView and I am attempting to implement action buttons from the custom calloutView. I am having trouble finding ways to detect the buttons tapped with respect to the calloutViews. </p>
  
  <p>Below are my implementation:</p>
  
  <pre><code>//At MapViewController
  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -&gt; 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    
      }
  }
  </code></pre>
  
  <p>I am able to detect the chat and delete buttons, but am unable to obtain the post with respect to the postArray or annotation. Is there a way to do so?</p>