Eman H Eman H - 1 year ago 92
iOS Question

Error when using unwind segue with UISearchController

I have a Navigation Controller with a UITableViewController.

When a user selects a cell on the TableViewController, it pushes to a new View Controller with a Table View inside. The user then selects a cell and the data gets passed back via an unwind segue.

The problem is I get this error when using a search bar before selecting the cell. Here is what the console says:

popToViewController:transition: called on <UINavigationController 0x7fc8ab856e00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

Code from View Controller I'm unwinding from:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath) as! MonsterSpriteCell
let monster = monsters[indexPath.row]
selectedMonster = monster
if isTeamBuilding {
performSegue(withIdentifier: "saveToTeamBuilderTableVC", sender: cell)
} else {
performSegue(withIdentifier: "showMonsterDetail", sender: self)

Here is the link the the project. The View Controller I'm unwinding from is Browse View Controller. The View Controller I'm unwinding to is TeamBuilderViewController

EDIT: I think it might be relevant to note that the segue unwinds to TeamBuilderTableViewController, which is a View Controller that is inside a container as a part of another view controller. Does anyone know if this might be why my unwind segue isn't working?

Answer Source

The SearchController is presenting an empty view controller
If you print the presentedViewController in didSelectRow.. when the search is active you will see a view controller.
that means you’re trying to perform segue from under the current presentation. You should dismiss the SearchController before performing any segues or presentations.

Also in this case you don’t need a SearchController since you’re only using the SearchBar for filtering.
Instead, put a SearchBar in the tableViewHeaderView and use its delegate to do the filtering and instead of checking if isActive to access the complete list vs. the filtered results, just put the whole array in the filter when the text is cleared and always access the filtered results.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download