1
votes

I have three views, each with its own view controller: VC1, VC2, VC3. The user will frequently switch back and forth between each of the three views, both forward and backward. Each view contains data: both shared from the previous view and data unique to that view. When the user goes back to a View that he has already visited, the data displayed on that view needs to be retained (the same data as he saw the last time he visited that view), and not set to the default values the first time he visited the view.

In the first view controller, VC1, I am using a prepare for segue to push data from VC1 to VC2 or VC3:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueToVC2” {
        let destinationViewController: VC2 = segue.destination as! VC2;
    destinationViewController.passedData1 = firstAmount
    destinationViewController.passedData2 = secondAmount
    destinationViewController.passedData3 = thirdAmount

    } else {
        let destinationViewController: VC3 = segue.destination as! VC3;
    destinationViewController.passedData1 = firstAmount
    destinationViewController.passedData2 = secondAmount
    destinationViewController.passedData3 = thirdAmount
    destinationViewController.passedData4 = fourthAmount
  }

By tapping the GO BACK button on each view, I return to the previous view:

@IBAction func goBackButtonPressed(_ sender: Any) {
    print("Back Button Pressed!")
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}

I am having trouble passing data backwards. And when I return to VC2 from VC1, data has been reset to 0. I have no segues going back from VC2 to VC1 or from VC3 to VC2. Would that be the cleanest way to pass the data back: to create another segue in Main.storyboard from VC2 to VC1 and then add another ‘if’ to my prepare for segue that checks for VC1?

I am passing ALL these variables back and forth between view controllers but only using some of them in each view controller. It seems like a waste and I don't think I am on the right track here.

Any help or suggestions?

2
Have a look at delegates!Rikh

2 Answers

2
votes

View controllers should never store data. They are responsible for coordinating between model objects and view objects. That's their whole point. The pattern you're looking for is called MVC (Model-View-Controller) and it's a core part of iOS development.

Move your data out of the view controllers and put it into model classes. Each view controller should fetch data out of the model, and send updates into the model. The only thing the view controllers should pass between themselves is what model objects to work on, and most of the time that only needs to pass in one direction (down the stack).

Delegation can be a useful tool here, and you can also investigate "unwind segues" which are built to help you send data upstream. But again, the data you should be sending is mostly references to the model, and the model itself needs to live outside the view controllers.

It's in Objective-C, but still one of the best simple examples from Apple on MVC design is TheElements, and is worth exploring as a basis. Even without reading the Objective-C, you can see how the various pieces fit together.

I haven't studied it as much as TheElements, but Lister claims to be a good demonstration of MVC patterns in Swift using modern iOS techniques.

0
votes

Why don't you call a delegate which passes the data to the view controller when you press back button.

Or if the data shared by all view controllers reflect the same value. Make a singleton class and use those values across the app.

example singleton class:

class SomeModel {
   static let shared = SomeModel()
   private init() {}
}