I want to pass data to the root view controller, which is ViewController after the previous one is dismissed, I chose to use delegation to solve this problem in this way:
Scheme:
ViewController -> SearchViewController
SearchViewController produces data and dismisses
class ViewController: UIViewController, IsAbleToReceiveData {
@IBAction func addButton_touchUpInside(_ sender: UIButton) {
performSegue(withIdentifier: "gotoSearchViewController", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "gotoSearchViewController" {
let vc2 = SearchViewController()
vc2.delegate = self
}
}
func passData(city: cityObject) {
print(city.cityName)
}
}
The protocol is declared:
protocol IsAbleToReceiveData: class {
func passData(city : cityObject)
}
In the SearchViewController I have:
weak var delegate: IsAbleToReceiveData?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let citySelected: cities?
citySelected = self.citiesQueried[indexPath.row]
RequestAPI.convertData(city: citySelected!) { (city) in
self.delegate?.passData(city: city)
}
dismiss(animated: true, completion: nil)
}
When I try to print the data in the function in ViewController nothing is printed even though in SearchViewController convertData method is returning a cityObject.
How can I pass the data to ViewController after dismissing?
Replace
with