未调用协议函数
Protocol Function not being called
请协助。我想要达到的目标
是当我点击特定的 collectionViewCell 时,ReportDetailsMapController 被推送并且我的报告 [indexPath.item] 从 MainController 发送到 ReportDetailsMapController.
协议:
protocol MainControllerDelegate: class {
func sendReport(data: ReportModel)
}
第一个 VC:
class MainController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var delegate: MainControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let report = reports[indexPath.item]
//When I print(report.name) here. Everything executes correctly
self.delegate?.sendReport(data: report)
self.navigationController?.pushViewController(ReportDetailsMapController(), animated: true)
}
}
第二VC:
class ReportDetailsMapController: UIViewController, MainControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mc = MainController()
mc.delegate = self
}
func sendReport(data: ReportModel) {
print(data.name)//This does not execute when ReportDetailsMapController is loaded.
print("Report sent")
}
}
报表模型:
class ReportModel: NSObject {
var name: String?
var surname: String?
var cellNumber: String?
}
该方法未被调用,因为您没有将要推送的视图控制器分配给 delegate
属性。
选择单元格后,您可以按以下顺序执行:初始化视图控制器并将其分配给委托,然后调用委托方法并推送该视图控制器:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let report = reports[indexPath.item]
//When I print(report.name) here. Everything executes correctly
let viewController = ReportDetailsMapController()
self.delegate = viewController
self.delegate?.sendReport(data: report)
self.navigationController?.pushViewController(viewController, animated: true)
}
但是,我认为更简单、更优雅的方法是简单地在 ReportDetailsMapController
上创建 属性 并在推送它之前注入它。
这里有一个与此相关的类似 question/answer:
请协助。我想要达到的目标 是当我点击特定的 collectionViewCell 时,ReportDetailsMapController 被推送并且我的报告 [indexPath.item] 从 MainController 发送到 ReportDetailsMapController.
协议:
protocol MainControllerDelegate: class {
func sendReport(data: ReportModel)
}
第一个 VC:
class MainController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var delegate: MainControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let report = reports[indexPath.item]
//When I print(report.name) here. Everything executes correctly
self.delegate?.sendReport(data: report)
self.navigationController?.pushViewController(ReportDetailsMapController(), animated: true)
}
}
第二VC:
class ReportDetailsMapController: UIViewController, MainControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mc = MainController()
mc.delegate = self
}
func sendReport(data: ReportModel) {
print(data.name)//This does not execute when ReportDetailsMapController is loaded.
print("Report sent")
}
}
报表模型:
class ReportModel: NSObject {
var name: String?
var surname: String?
var cellNumber: String?
}
该方法未被调用,因为您没有将要推送的视图控制器分配给 delegate
属性。
选择单元格后,您可以按以下顺序执行:初始化视图控制器并将其分配给委托,然后调用委托方法并推送该视图控制器:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let report = reports[indexPath.item]
//When I print(report.name) here. Everything executes correctly
let viewController = ReportDetailsMapController()
self.delegate = viewController
self.delegate?.sendReport(data: report)
self.navigationController?.pushViewController(viewController, animated: true)
}
但是,我认为更简单、更优雅的方法是简单地在 ReportDetailsMapController
上创建 属性 并在推送它之前注入它。
这里有一个与此相关的类似 question/answer: