微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用calloutAccessoryView作为新ViewController的segue发送方时,如何访问自定义批注属性?

如何解决使用calloutAccessoryView作为新ViewController的segue发送方时,如何访问自定义批注属性?

我有以下代码来准备进行测试:

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot",let jumpSpotCreatorControllerVC = segue.destination as? JumpSpotCreatorController {
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo",let jumpSpotInfoVC = segue.destination as? JumpSpotInfoController {
        if let senderAnnotationView = sender as? JumpSpotAnnotationView {
            jumpSpotInfoVC.titleLabel.text = senderAnnotationView.annotation?.title as? String
            jumpSpotInfoVC.imageView.image = senderAnnotationView.annotation.
        }
    }
}

我们在这里关注语句的“ else if”部分。我有一个自定义注释和注释视图。我使用用户单击以显示rightCalloutAccessoryView的.detaildisclosure版本的注释的属性,在要选择的视图控制器中填充标签和imageViews。但是,该发件人(rightCalloutAccessoryView的.detaildisclosure)仅允许我访问注释的标题和副标题。如您所见,当我进入image属性时,由于没有属性可访问,因此我停止了键入。如何访问自定义批注的属性

解决方法

难道您不能像image一样获得senderAnnotationView.annotation?.image,就像您要获得title一样吗?

PS:不要过多地依赖Xcode自动补全。有时效果并不理想。

,

好吧,我知道了。我要做的就是调整代码,以便使注释本身具有常量,并将其转换为我的自定义类。这是代码:

    override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot",let jumpSpotCreatorControllerVC = segue.destination as? JumpSpotCreatorController {
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo",let jumpSpotInfoVC = segue.destination as? JumpSpotInfoController {
        if let senderAnnotationView = sender as? JumpSpotAnnotationView {
            let senderAnnotation = senderAnnotationView.annotation as? JumpSpotAnnotation
            jumpSpotInfoVC.titleLabel.text = senderAnnotation?.title
            jumpSpotInfoVC.imageView.image = senderAnnotation?.image
            jumpSpotInfoVC.descriptionLabel.text = senderAnnotation?.description
            jumpSpotInfoVC.heightLabel.text = senderAnnotation?.estimatedHeight
            jumpSpotInfoVC.warningsLabel.text = senderAnnotation?.warnings
        }
    }
}

关键行是:让senderAnnotation = senderAnnotationView.annotation为? JumpSpotAnnotation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。