This is my first question here. I have TableViewController and I want to use two segues from one raw. One segue should work when you tapped a row, and the second is action from this row. Every of them need to show different ViewControllers and I don't understand how can I do that because I can't to create two segues from one row. The problem is that both of cases need to call prepare function and it called only with segue, and it does not called when I use performSegue.
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let editAction = UIContextualAction(style: .normal, title: "Add"){(_,_, completionHandler) in
self.performSegue(withIdentifier: "editQuiz", sender: self)
tableView.reloadData()
}
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(_,_, completionHandler) in
CoreDataManager.shared.deleteSomeQuizData(data: CoreDataManager.shared.quizzes[indexPath.row], indexNumber: indexPath.row)
self.navigationItem.title = "\(CoreDataManager.shared.quizzes.count) quizzes"
tableView.reloadData()
}
return UISwipeActionsConfiguration(actions: [deleteAction, editAction])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let indexPath = tableView.indexPathForSelectedRow else {return}
print(segue.source)
if segue.identifier == "editQuiz" {
guard let destination = segue.destination as? AddWordsViewController else{return}
}
if segue.identifier == "showQuiz" {
guard let destination = segue.destination as? QuizViewController else{return}
destination.from = Int(CoreDataManager.shared.quizzes[indexPath.row].from)
destination.to = Int(CoreDataManager.shared.quizzes[indexPath.row].to)
}
}
prepare(for segue:) works only when it called from row segue to another ViewController and it didn't called with performSegue. Also if I create both of segue from TableViewController to ViewControllers and don't call performSegue, transition doesn't work.
All of the segues identifiers set correctly.
Even if I try
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showQuiz", sender: self)
}
prepare(for segue:) is calling, but in
let editAction = UIContextualAction(style: .normal, title: "Add"){(_,_, completionHandler) in
self.performSegue(withIdentifier: "editQuiz", sender: self)
tableView.reloadData()
}
it does not calling.
It looks like you have almost everything setup correctly, except...
In your
prepare for seguecode, the first thing you do is check for the selected row:If you call
performSeguefrom your "Edit" action, the table view will NOT HAVE a selected row.A bit tough for me to test because I don't have all of your data management and destination controllers, but this should fix the issue (if you have your segues setup correctly in Storyboard):
As I mentioned in my comment: "To give yourself the most control, don't use segues in that situation..."
To do that, delete the segues from your Storyboard.
Instead, use code like this to instantiate the "destination" view controllers as needed: