Reputation: 4399
So, I have the following code snippet:
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
}()
But I am consistently getting the following error:
Value of type '(AllFiltersUberResetApplyTableViewCell) -> () -> AllFiltersUberResetApplyTableViewCell' has no member 'delegate
What is wrong with my code? Why is is it saying there is no member delegate when there clearly is a member?
Upvotes: 1
Views: 794
Reputation: 943
leftButton
will be initialised before UITableViewCell
is completely initialised and self
exists. So You cannot use self
inside var leftButton: UIButton
. Try making it a lazy var
to make sure that it gets called only when it is accessed for the first time.
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
}()
Upvotes: 1