swift - 如何为不同 ViewController 的 UITableViewCell 中的 UIButton 设置 IBAction?

标签 swift

我已经子类化了 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/

相关文章:

xcode - 更新到 Xcode 6.3 后通过 Parse 错误登录 Facebook

ios - 自 iOS 12.2 更新以来,应用程序崩溃没有崩溃报告

ios - 隐藏导航栏时更改单元格高度

ios - 通过编程使 SKSpriteNode 在横向模式下居中。 swift

ios - 从部分中包含多行的 tableView 中删除一行

ios - didUpdateLocations 总是用不同的坐标多次调用

swift - 如何在 swift 中模拟 realm-cocoa

ios - 具有输入和输出一维数据缓冲区的 Metal 计算着色器?

swift - 检测在虚拟机上运行

swift - 以编程方式更改 scn 对象的 Material