class MyView:UIViewController{
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(segue.destination)
let distinationVC = segue.destination as? MyTableController
distinationVC?.topConstaints.constant = 0
}
}
class MyTableController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var topConstaints: NSLayoutConstraint!
}
返回
print(segue.destination)
<myApp. MyTableController:0 x>
但提出例外
distinationVC?.topConstaints.constant = 0
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
有什么我做错了吗?
我不知道如何解决这个问题
访问topConstaints
中的prepare(for segue:)
将无法正常工作,因为尚未创建控制器视图。
要么在CGFloat
中创建一个MyTableController
变量,然后在topConstaints
中更改你的viewDidLoad
,或者在let _ = distinationVC.view
之后调用let distinationVC = ..
来触发distinationVC的loadView
。