问题描述
这是我的代码:
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 {
print("eriubvwribvuorlaeD")
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
print("YOYOYOYO")
}
}
}
对于第二个标识符为“ JumpSpotInfo”的脚本,我知道代码正在运行if let语句,因为在调试器中显示了print(“ eriubvwribvuorlaeD”),但是我不知道为什么在代码中如果不执行。有什么想法吗?
解决方法
这是一个很好的例子,为什么强制展开会非常有用。
强制解开与segue直接相关的所有可选内容。如果代码崩溃,则您犯了一个设计错误,可以立即修复。如果所有类和标识符都设置正确,则代码不会崩溃。
通过可选绑定,什么也不会发生,而且你也不知道为什么。
并且您不能直接将值分配给标签。您需要在目标视图控制器中声明属性,并在viewDidLoad
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
print("eriubvwribvuorlaeD")
let senderAnnotationView = sender as! JumpSpotAnnotationView
let senderAnnotation = senderAnnotationView.annotation as! JumpSpotAnnotation
// create the following properties in JumpSpotInfoController
jumpSpotInfoVC.title = senderAnnotation.title
jumpSpotInfoVC.image = senderAnnotation.image
jumpSpotInfoVC.description = senderAnnotation.description
jumpSpotInfoVC.height = senderAnnotation.estimatedHeight
jumpSpotInfoVC.warnings = senderAnnotation.warnings
print("YOYOYOYO")
}
}