I have: Model with Class GameBrain, ViewController that opens in segue TableViewController.
In TableViewController I need to edit var that is in GameBrain to 0. When I change its value there using gameBrain.myVar = 0 and print its value from TableViewController its 0 as expected, but when I go back to ViewController its unchanged.
I tried to change its value using delegate (code below), but it doesnt even print "IT WORKS!" just so I know its going the right direction.
What am I doing wrong? Why isn't function resetPlayerScore() called?
TableViewController:
protocol MenuTableViewControllerDelegate: AnyObject {
func resetPlayerScore()
}
class MenuTableViewController: UITableViewController {
weak var delegate: MenuTableViewControllerDelegate?
private let gameBrain = GameBrain()
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.row {
case 0:
delegate?.resetPlayerScore()
default:
break
}
}
}
Model:
extension GameBrain: MenuTableViewControllerDelegate {
func resetPlayerScore() {
print("IT WORKS!")
}
I wrote an example for you. viewController should implement the delegate not the GameBrain Model
check the code below i hope that what you are looking for
i created viewController and MenuTableViewController in the storyboard and the segue in the storyboard as well you have to set the delegate = viewController before navigating to the tableViewController and you can do that in this function prepare(for:sender:)