小编Mar*_*cía的帖子

委派不工作 Swift

我正在尝试在 Swift 上实现委托模式。该过程包括一个弹出窗口,该弹出窗口从文本视图上的文本选择中的 UIMenuItem 显示。这个弹出框是一个包含一些颜色的 TableViewController。当点击单元格(或颜色)时,所选文本的颜色从黑色变为所选颜色。我在发送类中有以下协议:

protocol SelectedColorDelegate {
func didSelectColorCell(color: UIColor)
}
Run Code Online (Sandbox Code Playgroud)

然后在发送类中我创建了这个属性:

var colorCellDelegate: SelectedColorDelegate?
Run Code Online (Sandbox Code Playgroud)

在作为发送类的tableViewController(popover)的didSelectRowAtIndexPath方法中,我分配了所需的参数:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let color = arrayOfColorValues[indexPath.row]
    self.colorCellDelegate?.didSelectColorCell(color: color)
}
Run Code Online (Sandbox Code Playgroud)

在我的 ViewController 接收类中,我设置了 SelectedColorDelegate 协议,并使用此方法符合它,旨在更改 textColor:

func didSelectColorCell(color: UIColor) {
    let textRange = noteTextView.selectedRange
    let string = NSMutableAttributedString(attributedString: noteTextView.attributedText)
    string.addAttribute(NSForegroundColorAttributeName, value: color, range: textRange)
    noteTextView.attributedText = string
    noteTextView.selectedRange = textRange
}
Run Code Online (Sandbox Code Playgroud)

但是最后一个方法永远不会被调用,点击弹出框的单元格什么也不做,我错过了什么或做错了什么?谢谢!!:)

protocols delegation uitableview ios swift

1
推荐指数
1
解决办法
7714
查看次数

标签 统计

delegation ×1

ios ×1

protocols ×1

swift ×1

uitableview ×1