Search code examples
swiftuitableviewsegueselectionediting

How to prevent segue transition when table view is editing in Swift?


I'm creating a Notes app clone based on the iOS Notes app (for a tutorial's challenge). I'm using the isEditing functionality to delete rows in the table view.
The first screen is connected to the second with a storyboard segue but if I am editing (that is, with the empty circles to the left of the rows), tapping on any row both selects and activates the transition. How can I avoid this?

I added an if isEditing { return } call inside the segue, inside the didSelectRowAt (which then I removed) but nothing of this works. The transition still happens regardless.

This is the setEditing one:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: true)
    tableView.setEditing(tableView.isEditing, animated: true)
    toolbarItems?[1].isEnabled.toggle()
    navigationItem.rightBarButtonItems?[1].isEnabled.toggle()
}

There is no other editing code apart from the button creation code let editButton = editButtonItem

The rest of the code is here.

Checking also with a Ray Wenderlich tutorial (the Checklist app) I would expect everything to work just fine but I get the transition triggered when "edit mode" is active.


Solution

  • Implement shouldPerformSegue(withIdentifier:sender:) in your view controller, and have it return false if you don't want the segue to be performed.