【问题标题】:Value of Type has no member Delegate error类型的值没有成员委托错误
【发布时间】:2020-08-11 17:18:09
【问题描述】:

所以,我有以下代码sn-p:

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 我的代码有什么问题?为什么明明有成员却说没有成员代表?

【问题讨论】:

    标签: swift delegates protocols


    【解决方案1】:

    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
      }()
    

    【讨论】:

    • 谢谢@Jithin,你成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多