我的 If let 语句失败了,我不确定为什么
My If let statement is failing and I'm not sure why
这是我的代码:
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")
}
}
}
对于第二个 segue,标识符为“JumpSpotInfo”,我知道代码正在执行 if let 语句,因为调试器中出现了 print(“eriubvwribvuorlaeD”),但我不知道为什么代码在 if let 内部不会执行。有什么想法吗?
这是强制展开非常有用的一个很好的例子。
强制解包所有与 segue 直接相关的选项。如果代码崩溃,您犯了 design 错误,可以立即修复。如果所有 类 和标识符设置正确,代码不得崩溃。
使用可选绑定什么也不会发生,你也不知道为什么。
并且您不能直接将值分配给标签。您需要在目标视图控制器中声明属性并在 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")
}
}
这是我的代码:
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")
}
}
}
对于第二个 segue,标识符为“JumpSpotInfo”,我知道代码正在执行 if let 语句,因为调试器中出现了 print(“eriubvwribvuorlaeD”),但我不知道为什么代码在 if let 内部不会执行。有什么想法吗?
这是强制展开非常有用的一个很好的例子。
强制解包所有与 segue 直接相关的选项。如果代码崩溃,您犯了 design 错误,可以立即修复。如果所有 类 和标识符设置正确,代码不得崩溃。
使用可选绑定什么也不会发生,你也不知道为什么。
并且您不能直接将值分配给标签。您需要在目标视图控制器中声明属性并在 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")
}
}