快速将数据从弹出窗口发送到另一个视图

时间:2018-12-09 02:29:30

标签: swift xcode

我正在尝试将数据从弹出视图发送到DataView。 它实际上有效!但是,当我返回到弹出视图来编辑文本时,它不会显示已输入并发送到DataView的文本。

我正在通过协议发送数据。

PopupView

protocol DataEnteredDelegate: class {
func userDidEnterInformation(data: String)}


@IBAction func DoneButton(_ sender: Any) {
    if let data = openTextView.text {
        delegate?.userDidEnterInformation(data: data)
        self.dismiss(animated: true, completion: nil)
    }

}

DataView

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "openSummary" {
        let sendingVC: popupViewController = segue.destination as! popupViewController
        sendingVC.delegate = self
    }
}


// Protocol receving data from popup
    func userDidEnterInformation(data: String) {
    addJobSum.text = data
}

1 个答案:

答案 0 :(得分:0)

关闭PopupViewController后,它将销毁,并且没有新实例能够知道旧实例作为Text View的文本值所具有的内容。

要解决此问题,请在PopupViewController内创建一个字符串属性,并使用诸如viewDidLoad()之类的方法将其值初始化为Text View的text属性:

class PopupViewController: UIViewController {
    var textData: String?

    /* more code */

    func viewDidLoad() {
        /* more code */

        if let data = textData {
            openTextView.text = data
        }
    }
}

然后,您必须在textData方法内(在显示之前)为prepare(for:)注入正确/所需的文本:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "openSummary" {
        let sendingVC = segue.destination as! PopupViewController
        sendingVC.delegate = self
        // This is the new line to be added: 
        sendingVC.textData = addJobSum.text
    }
}
相关问题