Search code examples
swiftxcodestoryboard

Variable not updating inside the view


For some reason, the variable Tags isnt being updated. When SelectedSauce is ran, it should run the function and then update the variable. But it's not being updated once it leaves the function. Im not sure what is wrong with this. When I change views, I pass a variable to selectedsauce from the previous view to here. im not sure if it helps or changes anything but I am using the Realm Database

class InfoUITableViewController: UITableViewController {


    var SelectedSauce: Sauce? {
        didSet {
            print("1st")
            AcquireData()
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        print("3rd")
        print("Loaded")

        tableView.register(UINib(nibName: "DetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "Super")
        tableView.dataSource = self

        //Returns nill even though i changed the variable in acquiredata
        print(Tags)
    }

    let realm = try! Realm()

    var Tags: List<NiceTags>?

    //MARK: - Data Manipulation

    func AcquireData() {
        print("2nd")
        if let Sauce = SelectedSauce {
            Tags = Sauce.tags
            //            print(Tags)
        }
            self.Tags = self.SelectedSauce?.tags
            print(self.Tags)
        tableView.reloadData()
    }


    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("4th")

        let cell = tableView.dequeueReusableCell(withIdentifier: "Super", for: indexPath) as! DetailsTableViewCell

        //This isn't running, and just uses the default text inside the label
        if let TheTags = Tags?[indexPath.row] {
            cell.Inflabel.text = TheTags.tags
        }


        return cell
    }




    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of row
        print("5th")
        //Returns 7 because tags is still nill
        return Tags?.count ?? 7
    }
}

Solution

  • Set SelectedSauce from tabbar like this

    override func viewDidLoad() { 
    super.viewDidLoad() 
    swipeAnimatedTransitioning?.animationType = SwipeAnimationType.sideBySide 
    // isCyclingEnabled = true 
    // Do any additional setup after loading the view. 
    print("order") 
    let first_vc = self.viewControllers?.first as! DetailViewController 
    let last_vc = self.viewControllers?.last as! InfoUITableViewController 
    first_vc.SelectedSauce = SelectedSauce 
    last_vc.SelectedSauce = SelectedSauce 
    }