当使用calloutAccessoryView作为分段到新视图控制器的发送方时,如何访问自定义注释属性



我有以下代码来准备我的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"部分。我有一个自定义的注释和注释视图。我正在分割到的视图控制器中填充标签和imageViews,使用用户单击的注释的属性来显示权利CalloutAccessoryView的.detailDisclosure版本。但是,发件人(.detailDisclosure of rightCalloutAccessoryView(只允许我访问注释的标题和副标题。正如您所看到的,当我到达image属性时,我停止了键入,因为没有可访问的属性。如何访问自定义注释的属性?

难道不能像现在获取title一样,通过senderAnnotationView.annotation?.image获取image吗?

附言:不要太依赖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=senderAnnuationView.annotation作为?JumpSpotAnnotation

最新更新