【发布时间】:2016-12-24 14:40:55
【问题描述】:
我正在尝试在 Swift 上实现委托模式。该过程包含一个弹出框,该弹出框在 textView 上的文本选择中从 UIMenuItem 显示。这个弹出框是一个包含一些颜色的 TableViewController。当一个单元格(或颜色)被点击时,选定的文本将其颜色从黑色更改为选定的颜色。我在发送类中有以下协议:
protocol SelectedColorDelegate {
func didSelectColorCell(color: UIColor)
}
然后在发送类中我创建了这个属性:
var colorCellDelegate: SelectedColorDelegate?
在作为发送类的tableViewController(popover)的方法didSelectRowAtIndexPath中,我分配了所需的参数:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let color = arrayOfColorValues[indexPath.row]
self.colorCellDelegate?.didSelectColorCell(color: color)
}
在我的 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
}
但是最后一个方法永远不会被调用,点击弹出框的单元格什么也没做,我错过了什么或做错了什么?谢谢!! :)
【问题讨论】:
-
你能用
didSelectRowAt在函数中设置断点并检查它是否崩溃吗? -
您是否将 ViewController 分配给 colorCellDelegate?
-
popover?.delegate = self ;确保将这些委托分配给 Viewcontroller。
-
@AdrianBobrowski 是的,它被调用了。
-
@MarilynGarcía 好的,然后现在用
didSelectRowAt在函数中打印self.colorCellDelegate我认为这个代表为零
标签: ios swift uitableview protocols delegation