Sebastian Considine Sebastian Considine - 14 days ago 8
iOS Question

UIButton in table cell "unrecognized selector sent to instance"

I have a button in a table cell, when pressed it crashes the app with the error:


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'


Here's the code:

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
}


I looked at the solutions here: Link1 and here Link2 but I get the same error every time.
The cell has its own .swift file where it was dragged as an outlet:
Cell.swift file

When the crash happens Xcode takes me to the AppDelegate.swift and shows this: crash goto

Does anyone know how to fix this?

Answer

Seems like the problem is about forwardPress, not forwardButton nor cellButtonPress. Did you check the Outlet Inspector in the Interface Builder?

On some interface element (maybe the cell when reading the debugger), you may have an outlet not linked in code called forwardPress. You perform the action on the element, IB looks for the forwardPress method, which does not exist => crash.