![](/img/trans.png)
[英]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.