如何使用 delegates
将 didTap
的值传递给 ReminderViewController
?这是我来自 CustomCell
的代码,其中包含 didTap
protocol BEMCheckBoxDelegate {
func checkBox(cell: CustomCell)
}
class CustomCell: UITableViewCell {
var delegate: BEMCheckBoxDelegate?
override func awakeFromNib() {
super.awakeFromNib()
}
func didTap(_ checkBox: BEMCheckBox) {
print("Checkbox is \(checkBox.on)")
self.delegate?.checkBox(cell: self)
}
然后在我的 ReminderViewController
中:
func checkBox(cell: CustomCell) {
guard let indexPath = self.tableView.indexPath(for: cell) else{
return
}
print("Button tapped on row \(indexPath.row)")
let task=tasks[indexPath.row]
task.checkDone="on"
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
最佳答案
也许你可以扩展你的协议(protocol)功能:
protocol BEMCheckBoxDelegate {
func checkBox(cell: CustomCell, isOn: Bool)
}
然后在 Cell 中,您可以这样调用它:
self.delegate?.checkBox(cell: self, isOn: checkBox.on)
并且不要忘记更改 ReminderViewController 中的函数:
func checkBox(cell: CustomCell, isOn: Bool) {
...
}
关于ios - 为什么我不能通过我的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025603/