Xcode: Passing Information from UITableViewController to UIViewController

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 through a sequel:

  • Can't Modify MPRemoteCommandCenter Actions when using MPMusicPlayerController
  • Xcode, Parse Crash Reporting, dSYM file not created
  • How to use Mailcore framework in my IOS project?
  • Xcode 5: failed to get the task for process
  • UITableView scrolling is very slow it use the network for every scrolling
  • Since when does Xcode no longer require forward method declarations and why?
  • override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "show" {
                var ctrl = segue.destination as! DetailViewController
                ctrl.information = _informationList[id]
            }
        }
    

    The id variable is set through:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            id = indexPath.row
    
        }
    

    Now in my UIViewController I change the information with:

    override func viewDidLoad() {
            super.viewDidLoad()
            setInformation(i: information)
        }
    

    Now my problem is, that if I press, lets say cell 2. It switches to the ViewController and shows Information of cell 1. Than I go back to the tableview and I press cell 3. Then it shows me cell 2.

    In short, it seems that the viewController is loaded (with the last information), before it sets the new information.

    Is there any better way to solve this?

    2 Solutions Collect From Internet About “Xcode: Passing Information from UITableViewController to UIViewController”

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

    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.