gpt4 book ai didi

ios - 协议(protocol)中的委托(delegate)函数未被调用

转载 作者:行者123 更新时间:2023-12-02 05:10:35 25 4
gpt4 key购买 nike

我有一个以前工作的委托(delegate)和协议(protocol),因为不再调用到 Swift 3 的转换。

protocol TaskCellDelegate {
func doneHit(_ cell : TaskCell)
}

class TaskCell : UITableViewCell {

var delegate : TaskCellDelegate?

@IBOutlet weak var label: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var _checkBox: M13Checkbox!


override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action: #selector(TaskCell.buttonClicked(_:)))
tap.numberOfTapsRequired = 1
_checkBox.addGestureRecognizer(tap)
_checkBox.isUserInteractionEnabled = true
_checkBox.markType = .checkmark
_checkBox.boxType = .circle
_checkBox.stateChangeAnimation = .expand(.fill)
}
func buttonClicked(_ sender:UITapGestureRecognizer) {
delegate?.doneHit(self)
}
}
正如您所看到的,当点击 _checkBox 时,它应该调用我的类中的函数 didHit (未添加,因为它似乎没有必要,但我可以),但我设置了一个断点,但它从未被调用。我已经设置了我的委托(delegate)并遵守了类(class)中的协议(protocol),但什么也没有发生。 doneHit 函数应该更新我的后端,但它没有被调用。如果您需要更多信息,我可以提供。

编辑 1:

class TasksTVC: UITableViewController, TaskCellDelegate {

func doneHit(_ cell:TaskCell) {
if let indexPath = self.tableView.indexPath(for: cell) {
task = tasksInSectionArray[indexPath.section][indexPath.row]
if task.done == false {
cell._checkBox.setCheckState(.checked, animated: true)
task.done = true
task.completedBy = user
cell.detailLabel.text = "Completed By: \(task.completedBy)"
cell.label.textColor = UIColor.gray
print("cell checked")
}
else {
cell._checkBox.setCheckState(.unchecked, animated: true)
task.done = false
task.completedBy = ""
cell.detailLabel.text = ""
cell.label.textColor = UIColor.black
print("cell unchecked")

}
fb.updateTaskDoneBool(ref, taskID: task.id, taskDone: task.done)
fb.updateTaskCompletedBy(ref, taskID: task.id, taskCompletedBy: task.completedBy)
}

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
cell.selectionStyle = .none
task = tasksInSectionArray[indexPath.section][indexPath.row]
cell.label.text = task.title
if task.done == true {
cell._checkBox.setCheckState(.checked, animated: true)
cell.detailLabel.text = "Completed By: \(task.completedBy)"
cell.label.textColor = UIColor.gray
}
else {
cell._checkBox.setCheckState(.unchecked, animated: true)
cell.detailLabel.text = ""
cell.label.textColor = UIColor.black

}
doneHit(cell)
cell.delegate = self
return cell
}}

最佳答案

看起来您没有正确设置 TaskCell 实例中的 delegate 属性,我将制作一个非常基本的示例,希望它可以帮助您解决问题:

结果(已编辑)

代码

TableViewController

import UIKit

protocol TaskCellDelegate {
func doneHit(_ cell: TaskCell)
}

class TableViewController: UITableViewController, TaskCellDelegate {
func doneHit(_ cell: TaskCell) {
let alert = UIAlertController(
title: "Info",
message: "button touched in cell",
preferredStyle: .alert
)
present(alert, animated: true, completion: nil)
}
}

extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TaskCell
cell.delegate = self // probably you forgot to set this part?

return cell
}
}

TaskCell(已编辑)

您可以使用 addTarget 方法为 UIControlEvents 附加事件处理程序,而不是创建一个新的 UITapGestureRecognizer 来附加到复选框 .valueChanged 值。

import UIKit
import M13Checkbox

class TaskCell: UITableViewCell {

var delegate: TaskCellDelegate?

@IBOutlet weak var checkbox: M13Checkbox!

override func awakeFromNib() {
super.awakeFromNib()

checkbox.addTarget(self, action: #selector(buttonClicked), for: .valueChanged)
}


func buttonClicked() {
delegate?.doneHit(self)
}

}

关于ios - 协议(protocol)中的委托(delegate)函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803780/

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