委派无效 Swift
Delegation not working Swift
我正在尝试在 Swift 上实施委托模式。该过程包含一个弹出窗口,该弹出窗口从 textView 上的文本选择中的 UIMenuItem 显示。这个弹出窗口是一个包含一些颜色的 TableViewController。当点击一个单元格(或颜色)时,所选文本的颜色从黑色更改为所选颜色。我在发送 class 中有以下协议:
protocol SelectedColorDelegate {
func didSelectColorCell(color: UIColor)
}
然后在发送 class 我创建了这个 属性:
var colorCellDelegate: SelectedColorDelegate?
发送class表ViewController(popover)的方法didSelectRowAtIndexPath中,我赋值了需要的参数:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let color = arrayOfColorValues[indexPath.row]
self.colorCellDelegate?.didSelectColorCell(color: color)
}
在我的接收class中,这是一个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
}
但是最后一个方法从未被调用,点击弹出窗口的单元格没有任何作用,我错过了什么或做错了什么?谢谢!! :)
你在 xxViewController 中做了:xxTableViewController.colorCellDelegate = self
?
并且您的委托声明应该是弱的:
weak var colorCellDelegate: SelectedColorDelegate?
在 PopOverTableViewController 中,设置应如下所示 -
class PopOverTableViewController: UITableViewController, SelectedColorDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.colorCellDelegate = self
}
}
首先将您的协议定义为仅适用于 类
protocol SelectedColorDelegate: class {
func didSelectColorCell(color: UIColor)
}
其次,我们希望我们的代表弱保留
weak var colorCellDelegate: SelectedColorDelegate?
最后在显示其他视图或在 viewDidLoad 中设置委托,例如:
class YourViewController: SelectedColorDelegate {
final override func viewDidLoad() {
super.viewDidLoad()
self.colorCellDelegate = self
}
}
我正在尝试在 Swift 上实施委托模式。该过程包含一个弹出窗口,该弹出窗口从 textView 上的文本选择中的 UIMenuItem 显示。这个弹出窗口是一个包含一些颜色的 TableViewController。当点击一个单元格(或颜色)时,所选文本的颜色从黑色更改为所选颜色。我在发送 class 中有以下协议:
protocol SelectedColorDelegate {
func didSelectColorCell(color: UIColor)
}
然后在发送 class 我创建了这个 属性:
var colorCellDelegate: SelectedColorDelegate?
发送class表ViewController(popover)的方法didSelectRowAtIndexPath中,我赋值了需要的参数:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let color = arrayOfColorValues[indexPath.row]
self.colorCellDelegate?.didSelectColorCell(color: color)
}
在我的接收class中,这是一个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
}
但是最后一个方法从未被调用,点击弹出窗口的单元格没有任何作用,我错过了什么或做错了什么?谢谢!! :)
你在 xxViewController 中做了:xxTableViewController.colorCellDelegate = self
?
并且您的委托声明应该是弱的:
weak var colorCellDelegate: SelectedColorDelegate?
在 PopOverTableViewController 中,设置应如下所示 -
class PopOverTableViewController: UITableViewController, SelectedColorDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.colorCellDelegate = self
}
}
首先将您的协议定义为仅适用于 类
protocol SelectedColorDelegate: class {
func didSelectColorCell(color: UIColor)
}
其次,我们希望我们的代表弱保留
weak var colorCellDelegate: SelectedColorDelegate?
最后在显示其他视图或在 viewDidLoad 中设置委托,例如:
class YourViewController: SelectedColorDelegate {
final override func viewDidLoad() {
super.viewDidLoad()
self.colorCellDelegate = self
}
}