Xcode: Passing Information from UITableViewController to UIViewController

后端 未结 2 1599
耶瑟儿~
耶瑟儿~ 2021-01-28 17:06

I have a UIViewController which should show me DetailInformations depending on what Cell was pressed in the UITableViewController.

For the moment I am passing them throu

相关标签:
2条回答
  • 2021-01-28 17:16

    Try using indexPathForSelectedRow in prepareForSegue as of it looks like that you have created segue from UITableViewCell to the Destination ViewController so that prepareForSegue will call before the didSelectRowAt.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "show" {
            var ctrl = segue.destination as! DetailViewController
            if let indexPath = self.tableView.indexPathForSelectedRow {
                ctrl.information = _informationList[indexPath.row]
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-28 17:17

    I am assuming based on what you are describing is that you used a segue in your Storyboard to link directly from the cell to the detail view controller. This is not what you want to do, as mentioned earlier, because you don't get the order of events you would expect. You could use the delegation design pattern for this, but assuming you want to stick with segues you need to make the "show" segue from the table VC itself to the detail VC. You then manually call the segue from the tableView didSelectRowAt code.

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        id = indexPath.row
        performSegue(withIdentifier: "show", sender: self)
    }
    

    Finally, you could then use an unwind segue when you come back to catch any data changes initiated in the detail VC.

    0 讨论(0)
提交回复
热议问题