使用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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...