【问题标题】:Swift Custom Map Call Out ErrorSwift 自定义地图调出错误
【发布时间】:2017-11-02 23:10:35
【问题描述】:

我之前使用过自定义地图标注,并且已经让它们发挥作用。太糟糕了,这不是那些情况,因为我一直在试图弄清楚为什么我不断收到此错误:“无法将 'NSKVONotifying_MKPointAnnotation' (0x1c0115960) 类型的值转换为 'ShopPeer.CustomBusinessPoint' (0x101159c68)。”我在“让 customAnnotation = view.annotation as!CustomBusinessPoint”这一行收到此错误。我以本教程为例:http://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/ ...另外,在下图中的编译器中查看出现了多少视图...不知道为什么会有这么多。

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else { return nil }
    let reuseId = "pin"
    if let pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? CustomBusinessCallOutAnnotatiion {
        return pinView
    } else {
        let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.pinTintColor = UIColor.red
        pinView.canShowCallout = false
        //pinView.rightCalloutAccessoryView = UIButton(type: UIButtonType.infoLight)

        return pinView
    }
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if view.annotation is MKUserLocation { return }

    let customAnnotation = view.annotation as! CustomBusinessPoint
    let views = Bundle.main.loadNibNamed("CustomBusinessCallOut", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomBusinessCallOut


    calloutView.businessName.text = customAnnotation.businessName


    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height * -0.0001)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

【问题讨论】:

    标签: ios swift mapkit mapkitannotation callout


    【解决方案1】:

    错误很明显,您的某些内容与类型不匹配。例如,视图不是自定义的,或者您没有覆盖自定义类中的 .annotation 字段。你需要寻找你的类,以确保视图是你的超类。调试视图是否为自定义。最后,阅读您正在关注的文章下的 cmets - 将会有更多人可能成为您特定问题的支持者

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多