如何将自定义属性从Mapbox批注传递到其标注视图?

酸门

我子类化MGLAnnotation以添加一个额外的属性,var tags如下所示:

class MasterMapAnnotation: NSObject, MGLAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var tags: String?

    init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?, tags: String?) {

        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
        self.tags = tags

    }

}


这是传递到地图的注释:

let annotation = MasterMapAnnotation(coordinate: CLLocationCoordinate2D(latitude: 50.0, longitude: 50.0), title: "Numero Uno", subtitle: "Numero Uno subtitle", tags: "#tag1 #tag2")
mapView.addAnnotation(annotation)


mapView委托调用自定义标注视图:

func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {

    return MasterMapCalloutView(representedObject: annotation)

}


因此,在标注视图子类(如下)中,如何访问此新属性tags

class MasterMapCalloutView: UIView, MGLCalloutView {

    var representedObject: MGLAnnotation
    let dismissesAutomatically: Bool = false
    let isAnchoredToAnnotation: Bool = true
    lazy var leftAccessoryView = UIView()
    lazy var rightAccessoryView = UIView()
    weak var delegate: MGLCalloutViewDelegate?

    required init(representedObject: MGLAnnotation) {
        self.representedObject = representedObject
        super.init(frame: .zero)
    }

    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // callout view delegate: present callout
    func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedView: UIView, animated: Bool) {

        if !representedObject.responds(to: #selector(getter: MGLAnnotation.title)) {
            return
        }

        view.addSubview(self)

        let title = representedObject.title
        let tags = representedObject.tags // how to access this?

    }

}


问题显然是子类(MasterMapAnnotation)拥有自定义tags属性,而不是其超类(MGLAnnotation),但我不知道如何(a)将presentatedObject设置为MasterMapAnnotation或(b)简单地访问MasterMapAnnotation自定义属性而无需重新配置协议。

马库斯

我可能会丢失一些东西,但是可以肯定的是,您只是检查是否可以将MGLAnnotation对象强制转换为MasterMapAnnotation:

guard let masterAnnotation : MasterMapAnnotation = representedObject as? MasterMapAnnotation else { return }

let tags : String? = masterAnnotation.tags

// etc

显然,除了使用卫兵以外,还有其他控制流的方法,但我希望您能掌握要点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义MKAnnotation标注视图?

如何将.xib界面设置为自定义标注视图?

使用Xib的自定义标注视图

使用 xip 自定义标注视图

如何处理自定义标注视图上的点击?

如何将自定义Dependecy属性绑定到控件的视图模型?

自定义注释标注视图还是自定义视图?

将自定义参数从视图传递到form_with

将自定义类的属性绑定到视图

从视图中,如何将自定义“选择”传递给表单的ChoiceField?

如何将自定义登录失败通知传递给 MVC4 中的视图

将自定义变量属性传递到foreach语句

将自定义属性传递到Aurelia组件

如何从ui-router中的视图将自定义数据传递到状态?

如何将自定义UICollectinViewCell注册到在xib文件的自定义视图中内置的UICollectionView中

如何将自定义对象从自定义身份验证提供程序传递到控制器?

如何将自定义指令属性传递给Angular 1.5中的自定义指令子元素?

如何将自定义数据传递到Highcharts图单击事件

如何将自定义信息从App Engine身份验证器传递到端点?

如何将自定义设置数据传递到src图像“ onClick”?

Rails / ActiveAdmin - 如何将自定义数据从控制器传递到内容中

如何将自定义变量从 ResetPasswordController 传递到重置刀片

如何将自定义模型的字段值传递到Qweb报表?

Angular - 如何将自定义错误消息从 restful api 传递到 Angular 前端

如何将自定义 cicd 变量传递到 yml 文件中

如何将自定义数据传递到Yii中的EDataTables

如何将自定义集合类从VB.NET传递到VB6?

如何将自定义路由器传递到Symfony捆绑包中的服务?

如何将自定义对象arraylist从活动传递到带有实例的片段