我試圖在Swift上實現委託模式。該過程包含一個彈出窗口,該窗口在textView的文本選擇中從UIMenuItem顯示。這個彈出窗口是一個包含一些顏色的TableViewController。點擊單元格(或顏色)時,所選文本會將其顏色從黑色更改爲所選顏色。我在發送級別以下協議:委派不工作Swift
protocol SelectedColorDelegate {
func didSelectColorCell(color: UIColor)
}
然後,在發送I類創建該屬性:
var colorCellDelegate: SelectedColorDelegate?
在tableViewController(酥料餅)的方法didSelectRowAtIndexPath方法即發送級別,我分配所需的參數:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let color = arrayOfColorValues[indexPath.row]
self.colorCellDelegate?.didSelectColorCell(color: color)
}
在作爲我的ViewController設置協議SelectedColorDelegate我接收類,並符合它瓦特第i這種方法,目的是改變文字顏色:
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'功能並檢查是否COLLED? –
您是否將ViewController分配給colorCellDelegate? –
popover?.delegate = self;確保將這些委託分配給Viewcontroller。 – Himanshu