简体   繁体   中英

Passing data to next view controller, but data disappears?

I have a tableview with cells that each contain a "Concept", CoreData Object. They all have an attribute called html, which are not nil (I checked that by printing in this tableview viewDidLoad() ). The problem is when I try to pass it in prepareForSegue this data kind of disappears.

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

The problem is in the viewDidLoad() the html is fine and not nothing, but in the prepareForSegue is prints nothing and in the PDFViewController it is also gone.

Does anybody know how to fix this?

try this..

    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
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM