dvd.trsnk
dvd.trsnk

Reputation: 21

Swift: How to save data to var in Model form segue?

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!")
    }

Upvotes: 0

Views: 55

Answers (1)

Ahmed Mohiy
Ahmed Mohiy

Reputation: 200

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

protocol MenuTableViewControllerDelegate: AnyObject {
func resetPlayerScore(gameBrain: GameBrain)
}

class MenuTableViewController: UITableViewController {


weak var delegate: MenuTableViewControllerDelegate?
private var gameBrain = GameBrain()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    return cell
}

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      tableView.deselectRow(at: indexPath, animated: true)
      gameBrain.value += 1
    
      switch indexPath.row {
      case 0:
          delegate?.resetPlayerScore(gameBrain: gameBrain)
          dismiss(animated: true)
      default:
          break
      }
  }
}

class GameBrain {
    var value: Int = 0
}


class ViewController: UIViewController {

  private var gameBrain = GameBrain()

  override func viewDidLoad() {
      super.viewDidLoad()
     // Do any additional setup after loading the view.
  }

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let destination = segue.destination as? MenuTableViewController {
          destination.delegate = self
      }
  }


}


extension ViewController: MenuTableViewControllerDelegate {
    func resetPlayerScore(gameBrain: GameBrain) {
        self.gameBrain = gameBrain
        print(gameBrain.value)
    }
}

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:)

Upvotes: 0

Related Questions