[英]Unable to add target to UIButton inside UITableViewCell using cell prototypes and storyboards
[英]Add target to UIButton inside UITableViewCell
我的CustomTableViewCell
有UIButton
并且我还调用了addTarget
:
let eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
然而,这个函数没有被调用(也在CustomTableViewCell
声明):
@objc func eyeButtonTapped(){
print("Hi")
}
当我点击button
时有一个“点击动画”但没有任何反应..
任何人都知道为什么会发生这种情况以及我如何适应它?
当声明你的按钮时, self
不存在。
在您的设置函数中添加目标,或将您的声明更改为lazy var
:
lazy var eyeButton: UIButton = {
let v = UIButton()
v.addTarget(self, action: #selector(eyeButtonTapped), for: .touchUpInside)
v.setImage(UIImage(named: "eyeOpen"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
创建按钮出口名称 cellbuttonName 或在 tableviewdelegate cellForRowAtIndexPath 中编写该代码以外的任何其他代码
[self.cellbuttonName addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.