gpt4 book ai didi

ios - 如何使用委托(delegate)将数据从页脚单元格传递到 View Controller ?

转载 作者:行者123 更新时间:2023-11-28 20:49:31 26 4
gpt4 key购买 nike

我一直试图将数据从 FoodEatenController(FEC) Footer 传递到 TotalCaloriesController。我现在拥有的代码在 TotalCalorieController(TCC) 的 calorieLbl 中没有显示任何内容。

我一直用来将数据从 FEC 传递到 TCC 的委托(delegate)不会将 FoodFooter calorieTotallbl 中的文本/字符串数据传递到 TEC calorieLbl

填充 FEC 单元格的数据从 Cloud Firestore 检索并从另一个 View Controller (FoodPickerController) 传入

FoodView

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")
}
}

最佳答案

func onTouchCaloireInfo(info:) 的末尾添加以下行

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

如果您想确保函数 onTouchCaloireInfo(info:) 被调用,只需添加以下行:

debugPrint("onTouchCaloireInfo")

然后检查它是否在 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
}
}
}

关于ios - 如何使用委托(delegate)将数据从页脚单元格传递到 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59299688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com