Send data to another View Controller by clicking on a row in table

Make sure View Controller inherits from UITableViewDelegate
When you are creating the segue from the table to the next view controller, select main page instead of the table cell and control click it to the next page.

1. In the View Controller receiving the data, give it a variable to hold the data, like task:
var thisTask : Task?

in ViewDidLoad, initialise UI elements with data
        toUpdateTF.text = thisTask?.nameOfTask
        toUpdateSW.isOn = thisTask?.isImportant as! Bool
     
Make sure the set the delegate to self:

theTableView.delegate = self
2. Connect the table and second view controller with a segue and give it a name.

3. Prepare for segue in the first view controller (with the table):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     
        if let id = segue.identifier
        {
            if(id == "updateDataSegue")
            {
                let newVC = segue.destination as! UpdateDataViewController
             
                newVC.thisTask = selectedTask
             
            }
        }
    }

4. Inside the function:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedTask = tasks[indexPath.row]
     
        performSegue(withIdentifier: "updateDataSegue", sender: self)
     
    }


Comments

Popular posts from this blog

Setting up a playground

Go to another page