简体   繁体   中英

How to pass data with delegate from footer cell to view controller?

Ive been stuck trying to pass data from the FoodEatenController(FEC) Footer to the TotalCaloriesController. The code that I have now it shows NOTHING in the calorieLbl of the TotalCalorieController(TCC).

The delegate that ive been using to pass the data from the FEC to the TCC does not pass the text/string data that is in the FoodFooter calorieTotallbl to the TEC calorieLbl

the data that populates the cells of the FEC is retrieved from Cloud Firestore and passed in from anotherView Controller (FoodPickerController)

美食观

import UIKit

class FoodEatenController: UIViewController{

    var selectedFood: FoodList!       // allows data to be passed into the VC

    // allows data to be sepearted into sections
    var foodItems: [FoodItem] = []
    var groupedFoodItems: [String: [FoodItem]] = [:]
    var dateSectionTitle: [String] = []

    @IBOutlet weak var tableView: UITableView!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let vc = segue.destination as? TotalCalorieController {

        }
    }
}

extension FoodEatenController: UITableViewDelegate, UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {
        return dateSectionTitle.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let date = dateSectionTitle[section]
        return groupedFoodItems[date]!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let foodCell = tableView.dequeueReusableCell(withIdentifier: "FoodCell") as! FoodCell

        let date = dateSectionTitle[indexPath.section]
        let foodItemsToDisplay = groupedFoodItems[date]![indexPath.row]
        foodCell.configure(withCartItems: fooditemsToDisplay.foodList)

        return foodCell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let foodHeader = tableView.dequeueReusableCell(withIdentifier: "FoodHeader") as! FoodHeader

        let headerTitle = dateSectionTitle[section]
        foodHeader.dateLbl.text = "Date: \(headerTitle)"

        return foodHeader
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let foodFooter = tableView.dequeueReusableCell(withIdentifier: "FoodFooter") as! FoodFooter

        let date = dateSectionTitle[section]
        let arrAllItems = groupedFoodItems[date]!

        var total: Float = 0
        for item in arrAllItems {
            let eaten = item.productList
            let selectedMeal = item.foodList.selectedOption
            if selectedMeal == 1 {
                total = total + (Float(eaten!.calorie))
            }
        }
        foodFooter.calorieTotal.text = String(subtotal!)
        foodFooter.delegate = self

        return foodFooter
        }

}

extension FoodEatenController: EatenFoodDelegate {
    func onTouchCaloireInfo(info: String) {
        let popUp = self.storyboard?.instantiateViewController(withIdentifier: "AdditionalCostsVC") as! TotalCalorieController
        popUp.calorieLbl.text = info
    }

}

import UIKit

protocol EatenFoodDelegate: class {
    func onTouchCaloireInfo(info: String)
}

class FoodFooter: UITableViewCell {

    weak var delegate: EatenFoodDelegate? = nil

    @IBOutlet weak var calorieTotal: UILabel!
    @IBOutlet weak var totalInfoBtn: UIButton!     

    @IBAction func totalOnClicked(_ sender: AnyObject) {
        self.delegate?. onTouchCaloireInfo(info: calorieTotal.text!)
    }
}

class TotalCalorieController: UIViewController, EatenFoodDelegate {

    func onTouchCaloireInfo(info: String) {
        calorieLbl.text = info
    }

    @IBOutlet weak var calorieLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()


    }
    @IBAction func returnButton(_ sender: Any) {
        dismiss(animated: true, completion: nil)
        print("Close Taxes and Fees")
    }    
}

Add the following line at the end of the func onTouchCaloireInfo(info:)

self.present(popUp, animated: true, completion: nil)

If you would like to be sure that the function onTouchCaloireInfo(info:) gets called, just add the following line:

debugPrint("onTouchCaloireInfo")

And check, if it prints the given string in the console of the Xcode


extension FoodEatenController: EatenFoodDelegate {
    func onTouchCaloireInfo(info: String) {
        debugPrint("onTouchCaloireInfo")
        let popUp = self.storyboard?.instantiateViewController(withIdentifier: "AdditionalCostsVC") as! TotalCalorieController

        self.present(popUp, animated: true) {
            popUp.calorieLbl.text = info
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM