如何在容器视图中嵌入TableView中更改变量?迅速

问题描述 投票:0回答:1
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

有什么我做错了吗?

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

ios swift uicontainerview
1个回答
1
投票

访问topConstaints中的prepare(for segue:)将无法正常工作,因为尚未创建控制器视图。

要么在CGFloat中创建一个MyTableController变量,然后在topConstaints中更改你的viewDidLoad,或者在let _ = distinationVC.view之后调用let distinationVC = ..来触发distinationVC的loadView

© www.soinside.com 2019 - 2024. All rights reserved.