ios - 如何使用委托将数据从一个视图控制器传递到另一个视图控制器?
问题描述
我试图使用委托将数据从一个视图控制器传递到另一个视图控制器
现在,当按下 CartCell 中的 modifyButton 时,我正在努力将数据从 CartVC 传递到 ModifyVC。这与我之前提出的前一个问题类似(参见下面的链接)。我只是在努力将数据传递给 ModifyVC,因为我一直收到错误消息Thread 1: Fatal error: Unexpectedly found nil 同时隐式展开 Optional 值并关闭模拟器
modifybtn 传递在 CartVC 中选择的 cel 的单元格数据
我不想使用 didSelectRowAt 来传递单元格数据,因为我使用 CartCell 中的 modifyBtn 来使用 ModifyDelegate 传递数据
我知道我接近完成这项工作的解决方案。我刚刚收到一个错误,它阻止我将数据传递给 ModifyVC
如何将数据从 TableViewCell 中的按钮传递到视图控制器?
class CartViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var selectedProduct: Items!
var modifyItems: Cart?
var cart: [Cart] = []
var groupedItems: [String: [Cart]] = [:]
var brands: [String] = []
@IBOutlet weak var cartTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //segue code for delegate
if let vc = segue.destination as? ModifyViewController {
vc.modifyItems = self.modifyItems
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return brands.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let brand = brands[section]
return groupedCartItems[brand]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cartCell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartCell
let brand = brands[indexPath.section]
let itemsToDisplay = groupedItems[brand]![indexPath.row]
cartCell.configure(withCartItems: itemsToDisplay.cart)
cartCell.modifyDelegate = self
cartCell.modifyItems = self.modifyItems
return cartCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader
let headerTitle = brands[section]
cartHeader.brandName.text = "Brand: \(headerTitle)"
return cartHeader
}
}
class ModifyViewController: UIViewController {
private var counterValue = 1
var lastSelectedWeightButton = RoundButton()
var modifyItems: Cart!
@IBOutlet weak var price1: UILabel!
@IBOutlet weak var price2: UILabel!
@IBOutlet weak var price3: UILabel!
@IBOutlet weak var weight1: UILabel!
@IBOutlet weak var weight2: UILabel!
@IBOutlet weak var weight3: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 2
formatter.numberStyle = .decimal
price1.text = "$\(formatter.string(for: modifyItems.cart.price1)!)" // getting the error right here that causes the simulator to close out and prevents me from viewing the modify VC
price2.text = "$\(formatter.string(for: modifyItems.cart.price2)!)"
price3.text = "$\(formatter.string(for: modifyItems.cart.price3)!)"
weight1.text = modifyItems.cart.weight1
weight2.text = modifyItems.cart.weight2
weight3.text = modifyItems.cart.weight3
}
}
side note: The CartVC cells data is populated from the HomeVc when an item is selected it posted as a cell in the CartVC. Items 类填充 HomeVC 中的单元格。
解决方案
更新 cellForRowAt 函数中的以下行:
cartCell.modifyItems = self.modifyItems
对此:
cartCell.modifyItems = itemsToDisplay
推荐阅读
- java - Java 和 Mac OS java -jar
- date - 在pyspark中将字符串转换为日期(格式年季度)
- html - 复选框重叠复选框标签
- javascript - 使用 document.createElement 创建时更改 html 元素在父标签中的位置
- css - 为什么 CSS 颜色在 PowerShell ConvertTo-HTML 电子邮件中消失?
- spring - 允许 bean 实现不定义 getBean 提供的参数
- python - 用 Python 脚本做 ssh 隧道。错误:“尝试打开 ssh 隧道时无法解析主机名”
- ggplot2 - 如何在ggplot r中的组合条形图和线图中标记线图?
- php - PHP 执行查询返回错误的输出
- bash - 剪切命令不重定向