ios - 表格单元格中的 UIButton "unrecognized selector sent to instance"

标签 ios swift swift3

我的表格单元格中有一个按钮,按下时它会使应用程序崩溃并出现错误:

unrecognized selector sent to instance 0x7f9a39840a00 2016-11-25 15:32:04.310 App Name[19161:1264937] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App_Name.routineCell forwardPress:]: unrecognized selector sent to instance 0x7f9a39840a00'

代码如下:

   internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return routineGroups.count
}

func cellButtonPress() {
    print("works")
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell:routineCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! routineCell

    cell.textLabel?.text = routineGroups[indexPath.row]

    cell.forwardButton.tag = indexPath.row
    cell.forwardButton.addTarget(self, action: #selector(routinesGroups.cellButtonPress), for: UIControlEvents.touchUpInside)

    return cell
}

我在这里查看了解决方案:Link1在这里 Link2但我每次都会遇到同样的错误。 该单元格有自己的 .swift 文件,将其作为导出拖动: Cell.swift file

当崩溃发生时,Xcode 将我带到 AppDelegate.swift 并显示:crash goto

有人知道如何解决这个问题吗?

最佳答案

问题似乎是关于 forwardPress,而不是 forwardButtoncellButtonPress。您检查过 Interface Builder 中的 Outlet Inspector 了吗?

在某些界面元素(可能是读取调试器时的单元格)上,您可能有一个未在名为 forwardPress 的代码中链接的 socket 。您对元素执行操作,IB 会查找 forwardPress 方法,该方法不存在 => 崩溃。

关于ios - 表格单元格中的 UIButton "unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808458/

相关文章:

ios - 移动到闭包内的另一个 Storyboard

iOS 通知和位置服务权限在启动时多次显示

swift - 在 Firebase 中将 observe 转换为 observeSingleEvent 时出错

iphone - 如何在运行时确定二进制图像架构?

ios - 如何在 QuickBlox 中创建临时用户?

ios - Native Express 广告未在我的 UITableView 中显示

ios - 使用 instantiateViewControllerWithIdentifier 和 performseguewithidentifier 有什么区别?

swift - 无法打开文件 “IMG_0001.JPG”,因为您没有查看它的权限

ios - 在 Swift 3 中添加阴影并使图像变圆的自定义 UIImageView 类

ios - 单击前景上的推送通知横幅后如何触发操作