按钮 从注释中单击发送对象 swift 4.



我在UIbutton中继承了注释类,例如:

 class MapButton : UIButton{
         var clickedAnnotation : annotations? = nil
    }

现在,我正在尝试在这样的注释中读取每个注释的属性:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            let colorPointAnnotation = annotation as! annotations
            pinView?.pinTintColor = colorPointAnnotation.pinColor
            let pinIsCurrentLocation = colorPointAnnotation.subtitle
            if pinIsCurrentLocation != "Current"
            {
            pinView?.canShowCallout = true
            }
            let smallSquare = CGSize(width: 30, height: 30)
            let button = MapButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
            button.setBackgroundImage(UIImage(named: "tasks"), for: .normal)
            button.clickedAnnotation = colorPointAnnotation
            button.addTarget(self, action: #selector(MapController.TaskDetail(_:)), for: .touchUpInside)
            pinView?.leftCalloutAccessoryView = button
        }
        else {
            pinView?.annotation = annotation
        }
        return pinView
    }

但是我收到错误"类型'地图控制器'在此行中没有成员'任务详细信息'",并且无法读取注释中的属性。

button.addTarget(self, action: #selector(MapController.TaskDetail(_:)), for: .touchUpInside)

按钮点击方式

func TaskDetail(_sender: MapButton){
        // Want to read the properties of each annotation when clicked on the map.
    }
如果您

使用的是Swift 4并且已在类MapController中声明了它,请确保在func TaskDetail(_sender: MapButton)之前添加@objc

例如

class MapController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        let colorPointAnnotation = annotation as! annotations
        pinView?.pinTintColor = colorPointAnnotation.pinColor
        let pinIsCurrentLocation = (colorPointAnnotation?.subtitle)!
        if pinIsCurrentLocation != "Current"
        {
            pinView?.canShowCallout = true
        }
        let smallSquare = CGSize(width: 30, height: 30)
        let button = MapButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
        button.setBackgroundImage(UIImage(named: "tasks"), for: .normal)
        button.clickedAnnotation = colorPointAnnotation
        button.addTarget(self, action: #selector(TaskDetail(_sender:)), for: .touchUpInside)
        pinView?.leftCalloutAccessoryView = button
    }
    else {
        pinView?.annotation = annotation
    }
    return pinView
  }
  @objc func TaskDetail(_sender: MapButton){
    // Want to read the properties of each annotation when clicked on the map.
  }
}

MapButton

class MapButton : UIButton{
  var clickedAnnotation : annotations? = nil
}

我希望这有帮助!

最新更新