Search code examples
iosswiftuicontainerview

How to Change Variable in embed TableVeiw in ContainerView? Swift


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!

}

return

 print(segue.destination)

<myApp. MyTableController:0 x>

but raise exception in

distinationVC?.topConstaints.constant = 0

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Is there something I'm doing wrong?

i don't know how to fix this


Solution

  • Accessing topConstaints in prepare(for segue:) won't work because the controller view has not been created yet.

    Either create a CGFloat variable in MyTableController then change your topConstaints in viewDidLoad or call let _ = distinationVC.view right after let distinationVC = .. to trigger distinationVC's loadView.