MichaelGofron
MichaelGofron

Reputation: 1410

Sending data between ViewControllers without segues

Is there a way to send data between ViewControllers without using segues?

Specifically I have two ViewControllers embedded in a TabBarViewController. One is called PlayingCardViewController and the other is called HighScoreViewController. I want to pass a class HighScore from PlayingCardViewController to HighScoreViewController. I want to transfer the data from PlayingCardViewController as soon as I press the redeal button in PlayingCardViewController but I don't want to transition to the HighScoreViewController as that would be jarring for the player.

I thought about using segues and holding the HighScores in an array and passing that to all the VC's that PlayingCardViewController is connected to but I realized that that seems overly complicated and there must be a simpler way to pass the data upon hitting the redeal button.

Some relevant links Passing Data between View Controllers

Upvotes: 0

Views: 85

Answers (1)

JoeFryer
JoeFryer

Reputation: 2751

I'm just going to do this with bullet points:

  • You could implement a custom tab bar controller (your own UITabBarController subclass), and use this instead of a basic UITabBarController.
  • Your PlayingCardViewController could have its own delegate protocol/property.
  • The delegate protocol could define a method like playingCardVC:didSetHighScore:.
  • Your tab bar controller would be the PlayingCardViewController's delegate.
  • The tab bar controller could keep a reference to the HighScoreViewController.
  • When your tab bar controller gets the playingCardVC:didSetHighScore:, it could pass whatever you want to your HighScoreViewController.

Upvotes: 1

Related Questions