首页 文章

Swift将数据从Popup发送到另一个视图

提问于
浏览
0

我正在尝试将数据从弹出视图发送到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)
    }

}

数据视图

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

    在PopupViewController被解除后,它被破坏,并且没有新实例能够知道旧实例具有的文本视图的文本值 .

    要修复它 - 在 PopupViewController 中创建一个字符串属性,并在某些方法中初始化Text View的text属性及其值,例如 viewDidLoad()

    class PopupViewController: UIViewController {
        var textData: String?
    
        /* more code */
    
        func viewDidLoad() {
            /* more code */
    
            if let data = textData {
                openTextView.text = data
            }
        }
    }
    

    然后,你必须在 prepare(for:) 方法内(在它出现之前)为 textData 注入正确/需要的文本:

    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
        }
    }
    

相关问题