Umit Sevim Umit Sevim -3 years ago 158
Swift Question

Pass data from Modal Viewcontroller to rootController with Swift 3 (don`t Segue)

I have a problem.

I need help. I have been looking for a solution for about 5 hours. Unfortunately without success.

My problem is that I have several storyboards and created without Segue.

I would love the selected Tableviewcell title pass to rootViewController.

Here is my code in my ModalViewTable:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let indexNumber = "\(indexPath.row)"

let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!
let storyboard = UIStoryboard(name: "addTime", bundle: nil)
var viewController = storyboard.instantiateViewController(withIdentifier: "addTimeVC") as! addTimeViewController
viewController.test = "TESTABCDEF"
passedValue = ("\(indexNumber)")
dismiss(animated: true, completion: nil)
}


I have a property
test
in my firstViewController. What is here wrong?
I've also tried with delegates and closures but without success. Please help me.

Answer Source

Try this instead:

let indexNumber = "\(indexPath.row)"
let indexPath   = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!

let viewController = navigationController!.presentingViewController as! addTimeViewController
viewController.test = "TESTABCDEF"
passedValue = ("\(indexNumber)")
navigationController!.dismiss(animated: true, completion: nil)

When you present a view controller modally using the present(_:animated) method, the view controller that was presented has the presentingViewController property set to the view controller that presented it. In other words, this property contains a reference to the previous view controller.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download