I have four view controllers and I am trying to send data between controllers via protocols although my two other protocols is working fine the last one is not working and I could not figure it out.
This is my first view controller that should send the data:
import UIKit
class ExercisesTableViewController: UITableViewController, ExerciseProtocol{
var exerciseToSend: Exercise? {
didSet{
print(exerciseToSend!) // This prints the result.
performSegue(withIdentifier: "showDetail", sender: self)
}
}
func getExercise() -> Exercise {
return exerciseToSend!
}
.
.
.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
exerciseToSend = exercises[indexPath.row]
}
.
.
.
}
This is the controller that should receieve the data
import UIKit
protocol ExerciseProtocol {
func getExercise() -> Exercise
}
class ExerciseDetailViewController: UIViewController {
var delegate:ExerciseProtocol?
override func viewDidLoad() {
print(delegate?.getExercise()) // This doesn't print the result.
super.viewDidLoad()
}
}
Add this inside ExercisesTableViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let des = segue.destination as? ExerciseDetailViewController else {return}
des.delegate = self
}