[英]Swift 3 - button in .xib custom tableviewcell is not performing action
I have a tableviewcontroller
, where i have created custom tableviewcell in a .xib files. 我有一个
tableviewcontroller
,我在.xib文件中创建了自定义tableviewcell。
The button (myButton) is used on myCustomTableViewCell.xib has an outlet to myCustomTableViewCell.swift 在myCustomTableViewCell.xib上使用按钮(myButton)。
I have set an action for the button in the TableViewController.swift file 我在TableViewController.swift文件中为按钮设置了一个动作
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("myCustomTableViewCell", owner: self, options: nil)?.first as! myCustomTableViewCell
cell.myButton.setTitle(arrayOfMyData[indexPath.row].myText, for:.normal )
//other code to set some other cell.attribute values like above (all works)
cell.myButton.actions(forTarget: "myAction", forControlEvent: .touchUpInside)
}
and elsewhere in the TableViewController.swift I have the action: 在TableViewController.swift中的其他地方,我可以执行以下操作:
func myAction(sender: UIButton){
print("pressed myButton")
}
but myAction is never called / "pressed myButton" is never printed. 但永远不会调用myAction /永远不会打印“ pressed myButton”。 What am I missing?
我想念什么?
You need to use addTarget(_:action:for:)
method to add the action for your button not the actions(forTarget:forControlEvent:)
. 您需要使用
addTarget(_:action:for:)
方法为按钮添加动作,而不是actions(forTarget:forControlEvent:)
。
The function actions(forTarget:forControlEvent:)
returns the actions that have been added to a control. 函数
actions(forTarget:forControlEvent:)
返回已添加到控件中的动作。 It doesn't add a target/action
. 它不会添加
target/action
。
cell.myButton.addTarget(self,action: #selector(myAction(sender:)), for: .touchUpInside)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.