类型的值没有成员委托错误
Value of Type has no member Delegate error
所以,我有以下代码片段:
protocol AllFiltersButtonsProtocol: AnyObject{
func didTapReset()
func didTapApply()
}
class AllFiltersUberResetApplyTableViewCell: UITableViewCell {
var delegate: AllFiltersButtonsProtocol?
var leftButton: UIButton = {
let leftButton = UIButton(.secondary, .large)
leftButton.setTitle("Reset", for: .normal)
leftButton.add(closureFor: .touchUpInside) { [self] in
self.delegate.didTapReset()
}
return leftButton
}()
但我一直收到以下错误:
Value of type '(AllFiltersUberResetApplyTableViewCell) -> () -> AllFiltersUberResetApplyTableViewCell' has no member 'delegate
我的代码有什么问题?为什么明明有会员却说没有会员代表?
leftButton
将在 UITableViewCell
完全初始化并且 self
存在之前被初始化。所以你不能在 var leftButton: UIButton
中使用 self
。尝试将其设为 lazy var
以确保仅在首次访问时调用它。
lazy var leftButton: UIButton = {
let leftButton = UIButton(.secondary, .large)
leftButton.setTitle("Reset", for: .normal)
leftButton.add(closureFor: .touchUpInside) { [self] in
self.delegate.didTapReset()
}
return leftButton
}()
所以,我有以下代码片段:
protocol AllFiltersButtonsProtocol: AnyObject{
func didTapReset()
func didTapApply()
}
class AllFiltersUberResetApplyTableViewCell: UITableViewCell {
var delegate: AllFiltersButtonsProtocol?
var leftButton: UIButton = {
let leftButton = UIButton(.secondary, .large)
leftButton.setTitle("Reset", for: .normal)
leftButton.add(closureFor: .touchUpInside) { [self] in
self.delegate.didTapReset()
}
return leftButton
}()
但我一直收到以下错误:
Value of type '(AllFiltersUberResetApplyTableViewCell) -> () -> AllFiltersUberResetApplyTableViewCell' has no member 'delegate
我的代码有什么问题?为什么明明有会员却说没有会员代表?
leftButton
将在 UITableViewCell
完全初始化并且 self
存在之前被初始化。所以你不能在 var leftButton: UIButton
中使用 self
。尝试将其设为 lazy var
以确保仅在首次访问时调用它。
lazy var leftButton: UIButton = {
let leftButton = UIButton(.secondary, .large)
leftButton.setTitle("Reset", for: .normal)
leftButton.add(closureFor: .touchUpInside) { [self] in
self.delegate.didTapReset()
}
return leftButton
}()