我已经子类化了 UITableViewCell。在这里,我为按钮制作了一个 socket 。单击该按钮后,它应该在不同的 ViewController 中执行一个函数。我试过这个:
override func awakeFromNib() {
super.awakeFromNib()
reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touchUpInside)
}
但是,这会因以下错误而崩溃:
[test.CreateChannelCell tappedOnReloadTableView]: 无法识别的选择器发送到实例 0x7fc1198b5200
函数存在,没有错字。为什么这不起作用?这个问题看起来是一样的,只是它不是用 swift 3.0 写的 How to set Action for UIButton in UITableViewCell
multiplayerChannelView 是包含 UITableView 的 View Controller 。我得到了一个单独的 .swift 文件,其中包含子类化的 UITableViewCell。
最佳答案
在 cellForRowAtIndexPath 中添加这个
cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);
并且在同一个 UIVeiwController 的任何地方定义函数如下
func tappedButton(sender : UIButton){
// Your code here
}
关于swift - 如何为不同 ViewController 的 UITableViewCell 中的 UIButton 设置 IBAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42479154/