Xcode - segue between table and detail view

I am stuck with the following code:

I am trying to build a segue between a table view and a detail view with an attached json file

error: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

What am I doing wrong?

Code:

    // This code is for the transfer to the next page

   

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

            let vc = storyboard?.instantiateViewController(withIdentifier: "ActionDetailViewController") as? ActionDetailViewController

            let action = result?.data3[indexPath.row]

            vc?.ActionTitleDetail.text = action?.actionTitle

              self.navigationController?.pushViewController(vc!, animated: true)

Also I need help to use more details from the json file on the detail view besides the elements in the table view.

Thanks

self.navigationController?.pushViewController(vc!, animated: true)

Force-unwrapping is dangerous.
Make that your vc is not nil, before you push it.

If it is nil, then solve that problem.

e.g.

if let vc = storyboard?.instantiateViewController(withIdentifier: "ActionDetailViewController") as? ActionDetailViewController {
	/// setup vc...
	self.navigationController?.pushViewController(vc, animated: true)
} else {
	print("Error: couldn't instantiate ActionDetailViewController")
}

what @robnotyou said plus:

There are 2 likely reasons:

  • you have not defined the Storyboard ID for the vc in storyboard (Identity inspector) or its is not ActionDetailViewController
  • the class of this VC is not ActionDetailViewController
Xcode - segue between table and detail view
 
 
Q