将数据从父控制器传递到Swift中的弹出控制器

时间:2016-10-28 03:44:36

标签: ios swift popup pass-data

我想在Swift中创建一个弹出控制器,但我无法将数据从父控制器传递到弹出窗口(在弹出窗口中设置标签文本)

代码:

let popUpPreload = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpOneID") as! PopUpViewController
popUpPreload.delegate = self
popUpPreload.lbTitlePopUp.text = "title"
popUpPreload.tvIntructions.text = "intructions"
self.addChildViewController(popUpPreload)
popUpPreload.view.frame = self.view.frame
self.view.addSubview(popUpPreload.view)
popUpPreload.didMove(toParentViewController: self)

设置标签弹出文本中的错误。

我在link

中提及

1 个答案:

答案 0 :(得分:1)

您无法从故事板中实例化任何ViewController,然后立即尝试在其插座上设置属性。在调用ViewDidLoad之前,它们都是零。当您调用实例化视图控制器时,它会加载XML,创建ViewController及其所有子视图,然后使用键值观察将所有出口设置为它创建的视图。然后在此过程完成后调用ViewDidLoad。

您需要在目标视图控制器上写入常规变量。

    let popUpPreload = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpOneID") as! PopUpViewController
    popUpPreload.delegate = self
    popUpPreload.titleText = "title"

然后在ViewController的ViewdidLoad函数中,将诸如label.text的outlet属性分配给变量。

    var titleText = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = titleText
    }
相关问题