Can only pass data between 2 of 3 View Controllers

Hi,

 

I´m making a simple orderplacement app. This app uses 3 View Controllers: Menu1, Menu2 and Checkout. (Menu2 was recently added, and is the source of the problem) I pass data between Menu VC an Checkout with a Swift file called orderModel. This has been working perfectly for a long time now, but when I added a new VC, Menu2, I got problems. Menu2 won´t pass data to Checkout VC. I have done the 2 menuVC´s identical. Just with different variables.

 

Menu1:

class menu1ViewController: UIViewController {

var myOrder = OrderModel()
var tomatoes = "Tomatoes"

@IBAction func item1(_ sender: UIButton) {
myOrder.menu1Tomatoes = tomatoes //Set text to item
myOrder.addToMenu1Tomatoes += 1 //Add amount of tomatoes odered
}
override func viewDidLoad() {
        super.viewDidLoad()
let barViewControllers = self.tabBarController?.viewControllers
let svc = barViewControllers![1] as! checkOutViewController
svc.myOrder = self.myOrder  //shared model
}

 

Menu2:

class menu2ViewController: UIViewController {
        var myOrder = OrderModel()
  var bacon = "Bacon"

    @IBAction func item20(_ sender: UIButton) {

        myOrder.menu2Bacon = bacon
        myOrder.addToMenu2Bacon += 1
    }
override func viewDidLoad() {
        super.viewDidLoad()
let barViewControllers = self.tabBarController?.viewControllers
let svc = barViewControllers![1] as! checkOutViewController
svc.myOrder = self.myOrder  //shared model
}

 

CheckOut:

class checkOutViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    var myOrder: OrderModel?

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
/
        orderLabel.text = myOrder?.currentOrder()
}

 

 

OrderModel:

 

import UIKit
class OrderModel: NSObject {
//item1
var menu1Tomatoes:String = ""
var addToMenu1Tomatoes:Int = 0

//item20
var menu2Bacon:String = ""
var addToMenu2Bacon:Int = 0


//Ready sending to orderLabel in CheckOut
    func item1Tomatoes() -> String {
        if addToMenu1Tomatoes >= 1 {
            return ("(addToMenu1Tomatoes) (menu1Tomatoes + "")")
        }
        return ""
    }

func item20bacon() -> String {
        if addToMenu2Bacon >= 1 {
            return ("(addToMenu2Bacon) (menu2Bacon + "")")
        }
        return ""
    }

func currentOrder() -> String{ //Return a string with the current order
        return item1Tomatoes() + item20Bacon()
}

 

The data is passed from menu2 to orderModel. Because if I print(myOrder.item1Tomatoes()) when item20 button is pressed, the data prints correctly.

I hope someone can help me here.

Thank you

Powered by WPeMatico

Skip to toolbar