Search code examples
iosswiftsegueuitableviewrowaction

Trigger segue from UITableViewRowAction


I've read many SO posts about this problem, but unfortunately none of the answers have solved my issue. I have a UIViewController with a TableView inside of it (I set it up this way for a specific UI format I wanted), and as of now if the user swipes to the left, a delete and edit button show up. My delete action works fine, but every iteration I've tried of edit so far has given me a SIGABRT / terminating with uncaught exception of type NSException error.

My ViewController class has the UIViewController, UITableViewDataSource, and UITableViewDelegate protocols added. The code for my tableView(editActionsForRowAt:) function is as follows:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    // Delete action
    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete", handler: { (action, indexPath) -> Void in

        // Delete action code works fine

    })

    deleteAction.backgroundColor = GlobalPropertyKeys.LovalyticsRed

    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Edit") {(action, indexPath) -> Void in

        // Will explain what I've tried so far below this

        print("Edit tapped.")
    }

    editAction.backgroundColor = GlobalPropertyKeys.LovalyticsBlue

    return [deleteAction, editAction]

}

All the answers I found on SO involved variations of creating the segue in IB from my ViewController to the destination screen, setting an identifier for the segue, and then calling that segue using performSegue(withIdentifier:sender:).

So far I have tried:

1.

    if let topController = UIApplication.topViewController() {
        topController.performSegue(withIdentifier: "segueToEditListItem", sender: topController)
    }

2.

    self.performSegue(withIdentifier: "segueToEditListItem", sender: self)

3.

    tableView.performSegue(withIdentifier: "segueToEditListItem", sender: tableView)
  1. Tried connecting the segue from the TableView instead of from the ViewController

And probably a few more variations of the above that I deleted instead of commenting them out. Each time, I get a SIGABRT error when clicking edit, which I always associate with incorrectly connected @IBOutlets. I checked to make sure the segue was connected correctly, and when I remove any mention of the segue my app works fine (the edit button prints "Edit tapped."), so that makes me think there's a problem with my ViewController not recognizing the segue in my .swift file.

I've spent several hours trying to figure this out and have failed pretty miserably, so any help / advice would be greatly appreciated.


Solution

  • The self.performSegue(withIdentifier: "Identifier", sender: self) should works.
    If your problem is about @IBOutlets you should check your Outlets again.
    And if you can write your console log where there is description of your crash.