无法在TableViewControllers之间传递数据

时间:2018-12-25 04:03:22

标签: ios swift segue

圣诞快乐!我目前正在制作一个“提醒”应用。我的“辅助” TableView无法将数据传递回“主” TableView。 (请参见下图)。我需要将数据传递回“主” TableView中,以便可以将其保存并显示到另一个TableView中,以便用户可以看到他们输入的任务。

My Storyboard, segue names in Red

IconTableViewController(和ShowTableViewController)内部的代码:

var userImage: String = ""
var userTitle: String = ""
var userDesc: String = ""
var frequency: String = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let navigationViewController = segue.destination as? UINavigationController {

        guard let viewController = navigationViewController.topViewController as? NewItemTableViewController else { return }

        let userImageNew = userImage
        let userTitleOld = userTitle
        let userDescOld = userDesc
        let frequencyOld = frequency

        viewController.selectedImage = userImageNew
        viewController.userTitle = userTitleOld
        viewController.userDesc = userDescOld
        viewController.selectedFrequency = frequencyOld
    }
}

NewItemTableViewController内部的代码:

var selectedImage: String = ""
var selectedFrequency: String = ""
var userTitle: String = ""
var userDesc: String = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showSelect" {
        let userImageOld = selectedImage
        let userTitleNew = titleTextField.text
        let userDescNew = descTextView.text
        let frequencyOld = selectedFrequency

        let viewController = segue.destination as! ShowTableViewController
        viewController.userImage = userImageOld
        viewController.userTitle = userTitleNew!
        viewController.userDesc = userDescNew!
        viewController.frequency = frequencyOld
    }

    if segue.identifier == "iconSelect" {
        let userImageOld = selectedImage
        let userTitleNew = titleTextField.text
        let userDescNew = descTextView.text
        let frequencyOld = selectedFrequency

        let viewController = segue.destination as! IconTableViewController
        viewController.userImage = userImageOld
        viewController.userTitle = userTitleNew!
        viewController.userDesc = userDescNew!
        viewController.frequency = frequencyOld
    }
}

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

似乎NewItemTableViewController尚未在导航堆栈的顶部viewController上。尝试以相同的方式访问它,

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let viewController = segue.destination as? NewItemTableViewController {

        let userImageNew = userImage
        let userTitleOld = userTitle
        let userDescOld = userDesc
        let frequencyOld = frequency

        viewController.selectedImage = userImageNew
        viewController.userTitle = userTitleOld
        viewController.userDesc = userDescOld
        viewController.selectedFrequency = frequencyOld
    }
}

您应该在上述方法prepare(for segue内放置一个断点,并查看是否正在调用它。因为我觉得您没有unwind segue,因为这些viewControllers只是被推送到导航堆栈中。因此,在那种情况下,您还可以通过从导航堆栈访问viewWillDisappear来使用NewItemTableViewController来设置数据。如下所示,

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if let vc = self.navigationController?.viewControllers.first(where: { $0 is NewItemTableViewController }) as? NewItemTableViewController {
        // Set data.
    }
}

答案 1 :(得分:0)

您应该使用<div class="wrapper"> <img class="stars" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/867725/stars.png " alt="" /> <img class="tress" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/867725/trees.png " alt="" /> <img class="clouds" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/867725/clouds.png" alt="" /> <canvas></canvas> </div>将数据推回到Delegate。您可以引用以下线程:Pass data back to previous viewcontrolle