如何在viewDidLoad发生之前发送数据?

问题描述

我正在以编程方式将数据发送到视图控制器。在加载之前,此数据必须是文本字段的值。但是,当我在没有顺序的情况下发送此数据时,将调用viewDidLoad,然后发送我的数据。

let formController = UINavigationController(rootViewController: SampleFormViewController())
if UIDevice.current.userInterfaceIdiom == .pad
{
    formController.modalPresentationStyle = .formSheet
}

let childViewController = formController.viewControllers[0] as! SampleFormViewController

print("inside sending data to the edit",medicationToEdit)

childViewController.id = self.id
childViewController.drugName = medicationToEdit
childViewController.frequency = frequencyToEdit
childViewController.dosage = dosagetoEdit

self.present(formController,animated: true,completion: nil) 

在SampleViewController中的viewDidLoad内部

print(self.frequency)
print(self.dosage)
print(self.drugName)

这些值为空。

如何解决此问题?我没有为此创建一个情节提要控制器,所以不能使用segues。

谢谢!

解决方法

您要在childViewController中填充变量,但是要呈现一个不同的控制器...

self.present(formController,动画:true,完成:无)

这需要更改。