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

标签 ios swift

我继承了 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
    }

但是我在这一行中收到错误“Type 'MapController' has no member 'TaskDetail'”,并且无法读取注释中的属性。

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 并且已在其中声明,请确保在 func TaskDetail(_sender: MapButton) 之前添加 @objcMapController

例如

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
}

希望这会有所帮助!

关于ios - 从注释中单击按钮发送对象 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49040539/

相关文章:

ios - 在 SwiftUI 中删除 LazyVStack 和 ForEach 中的行

iphone - 使用 Core Graphics 以 30-60 fps 的速度手动移动/旋转东西

ios - 当 Swift 中涉及 2 个对象时如何重用一堆代码

ios - 将用户重定向到 iOS facebook 应用程序而不是浏览器窗口进行身份验证

iphone - 如何在 iOS 上使用 Facebook 的转化跟踪像素

ios - 使用未声明的类型 'NSXMLElement' 错误

ios - 单元格的父vc中的Swift -AVPlayer'KVO导致Xcode卡住

swift - 当我点击 UIButton 时图像不会改变

ios - NSSet 中的第一个实体具有较短的 ID,并且不保留值更改

swift - 使用 Swift 编译 Latex 代码