[英]Add target to UIButton inside UITableViewCell
I have UIButton
inside my CustomTableViewCell
and I also call addTarget
:我的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
}()
However this functions is not being called (also declared inside CustomTableViewCell
):然而,这个函数没有被调用(也在CustomTableViewCell
声明):
@objc func eyeButtonTapped(){
print("Hi")
}
There is a "tap-animation" when I click the button
but nothing happens..当我点击button
时有一个“点击动画”但没有任何反应..
Anyone know why this happens and how I can fit it ?任何人都知道为什么会发生这种情况以及我如何适应它?
When declaring your button, self
doesn't exist at that point.当声明你的按钮时, self
不存在。
Either add the target in your setup func, or change your declaration to a lazy var
:在您的设置函数中添加目标,或将您的声明更改为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
}()
Create Button Outlet Name cellbuttonName or anyother than write that code in tableviewdelegate cellForRowAtIndexPath创建按钮出口名称 cellbuttonName 或在 tableviewdelegate cellForRowAtIndexPath 中编写该代码以外的任何其他代码
[self.cellbuttonName addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside]; [self.cellbuttonName addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.