如何使用委托将数据从一个视图控制器传递到另一个视图控制器?
how to use delegate to pass data to from one View Controller to another?
我正在尝试使用委托将数据从一个视图控制器传递到另一个视图控制器
现在,当按下 CartCell 中的 modifyButton 时,我正在努力将数据从 CartVC 传递到 ModifyVC。这个模型类似于我之前问过的一个问题(参见下面的 link)。我只是在努力将数据传递给 ModifyVC,因为我不断收到错误提示 Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 并关闭模拟器
modifybtn 为在 CartVC 中选择的 cel 传递单元格数据
我不想使用 didSelectRowAt 传递单元格数据,因为我使用 CartCell 中的 modifyBtn 来使用 ModifyDelegate 传递数据
我知道我已经接近完成这项工作的解决方案了。我只是收到一个错误,阻止我将数据传递给 ModifyVC
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
}
}
旁注:当一个项目被选中时,CartVC 单元格数据从 HomeVc 填充,它作为单元格发布在 CartVC 中。项目 class 填充 HomeVC 中的单元格。
更新 cellForRowAt 函数中的以下行:
cartCell.modifyItems = self.modifyItems
对此:
cartCell.modifyItems = itemsToDisplay
我正在尝试使用委托将数据从一个视图控制器传递到另一个视图控制器
现在,当按下 CartCell 中的 modifyButton 时,我正在努力将数据从 CartVC 传递到 ModifyVC。这个模型类似于我之前问过的一个问题(参见下面的 link)。我只是在努力将数据传递给 ModifyVC,因为我不断收到错误提示 Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 并关闭模拟器
modifybtn 为在 CartVC 中选择的 cel 传递单元格数据
我不想使用 didSelectRowAt 传递单元格数据,因为我使用 CartCell 中的 modifyBtn 来使用 ModifyDelegate 传递数据
我知道我已经接近完成这项工作的解决方案了。我只是收到一个错误,阻止我将数据传递给 ModifyVC
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
}
}
旁注:当一个项目被选中时,CartVC 单元格数据从 HomeVc 填充,它作为单元格发布在 CartVC 中。项目 class 填充 HomeVC 中的单元格。
更新 cellForRowAt 函数中的以下行:
cartCell.modifyItems = self.modifyItems
对此:
cartCell.modifyItems = itemsToDisplay