将数据传递给下一个视图控制器,但数据消失了吗?

时间:2017-11-09 11:12:16

标签: ios swift xcode

我有一个包含单元格的tableview,每个单元格都包含一个“Concept”CoreData Object。它们都有一个名为html的属性,它不是nil(我通过在这个tableview viewDidLoad()中打印来检查它。问题是,当我尝试在prepareForSegue中传递时,这种数据会消失。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let indexPath = tableView.indexPathForSelectedRow
        if segue.identifier == "toConcept"{
        let nc = segue.destination as! UINavigationController
        let vc = nc.viewControllers.first as! PDFViewController

        print(concepts[(indexPath?.row)!].html
        //This prints nothing.

        vc.html = concepts[(indexPath?.row)!].html
        }
    }

问题在于viewDidLoad() html很好而且没什么,但是在prepareForSegue中没有打印任何内容,而在PDFViewController它也消失了。

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个..

    func tableView(_ messagesListTableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "toConcept", sender: indexPath)
    print(indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toConcept"{
let indexPath = sender as? IndexPath
        let nc = segue.destination as! UINavigationController
        let vc = nc.viewControllers.first as! PDFViewController

        print(concepts[(indexPath?.row)!].html
        //This prints nothing.

        vc.html = concepts[(indexPath?.row)!].html
        }

}