首页 > 解决方案 > 如何更改 ContainerView 中嵌入 TableVeiw 中的变量?迅速

问题描述

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

有什么我做错了吗?

我不知道如何解决这个问题

标签: iosswiftuicontainerview

解决方案


由于尚未创建控制器视图,因此无法topConstaints访问。prepare(for segue:)

要么创建一个CGFloat变量,MyTableController然后更改你topConstaints的变量,viewDidLoad要么立即调用let _ = distinationVC.viewlet distinationVC = ..触发 distinationVC's loadView


推荐阅读