3
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "ExerciseMenuCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExerciseOptionTableViewCell
    let currentWorkout = workouts[indexPath.row]
    cell.nameLabel!.text = currentWorkout.name
    cell.photoImageView.image = currentWorkout.filename

    cell.startWorkout.tag = indexPath.row
    cell.startWorkout.addTarget(self, action:Selector("workoutAction:"), forControlEvents: .TouchUpInside)

    cell.infoWorkout.tag = indexPath.row
    cell.infoWorkout.addTarget(self, action:Selector("infoAction:"), forControlEvents: .TouchUpInside)

    return cell
    }

Both startWorkout and infoWorkout cause the application to crash with the error message 'unrecognised selector sent to instance'.

Example of code within the button actions. I am trying to return the indexPath of the button so I can then act on that.

@IBAction func workoutAction(sender: AnyObject) {
    let buttonTag = sender.tag
    print(buttonTag)

}

exact error message:

016-06-17 18:34:30.722 Exercises[4711:245683] - [Exercises.ExerciseMenu beginWorkout:]: unrecognized selector sent to instance 0x7fb47874a4b0 2016-06-17 18:34:30.727 Exercises[4711:245683] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Exercises.ExerciseMenu beginWorkout:]: unrecognized selector sent to instance 0x7fb47874a4b0'

Ryan Hampton
  • 319
  • 1
  • 4
  • 21
  • More info needed: 1) What is the exact message in crash log ? 2) Where did you write the `workoutAction` and `infoAction` methods ? – Midhun MP Jun 17 '16 at 17:23
  • The methods are written within the same view controller as the cellForRowAtIndexPath method. I'll edit the post to show more of the error message. – Ryan Hampton Jun 17 '16 at 17:24
  • Is that the error message you require? – Ryan Hampton Jun 17 '16 at 17:37
  • The error shows that, the crash was happening due to `beginWorkout` method. So I think the crash is happening somewhere else or you edited the actual code and forgot to update the crash log! – Midhun MP Jun 17 '16 at 17:41
  • beginWorkout is the following view controller. so the error would be in the segue between them? – Ryan Hampton Jun 17 '16 at 17:50
  • You've to use the #selector syntax like I showed you and use `thatclass.beginWorkout(:_)` as the selector. – Mtoklitz113 Jun 17 '16 at 17:53
  • @RyanHampton: Can't say what will be the exact issue without seeing that part of your code. But the issue is definitely due to calling `beginWorkout` method on `ExerciseMenu` object. – Midhun MP Jun 17 '16 at 17:59

1 Answers1

7

A button within a custom cell cannot call an action that is in the housing view controller. You need to:

1) Move the @IBaction function to the custom cell class

2) Remove the add target code from 'cellFromRowAtIndexPath' and either write that in your custom cell(if you do this you don't need to write @IBAction) or create the connection from the button in storyboard to the @IBAction function

3) Create a delegate for your custom cell Custom UITableViewCell delegate pattern in Swift

4) Call the delegate from your custom cell for the function you implement in the view controller **Don't for get you need cell.delegate = self or it will crash when the delegate gets called

ex:

CustomCell.swift

protocol CustomCellDelegate {
    func pressedButton()
}

class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate!

    @IBAction func buttonPressed(sender: UIButton) {
        delegate.pressedButton()
    }
}

ViewController.swift

class CustomClass: UIViewController, CustomCellDelegate {

    func pressedButton() {
        // Perform segue here
    }
}
brl214
  • 527
  • 1
  • 6
  • 16
  • Thanks for your reply. I understand steps 1 & 2 but am struggling with 3 & 4. I basically want the button to segue to a chosen view controller. – Ryan Hampton Jun 18 '16 at 11:14
  • @RyanHampton I updated the answer. Let me know if that makes sense. – brl214 Jun 21 '16 at 13:52