What I am trying to do, is to pop all previous view controllers from the stack up to the 'menu' controller. I have a piece of code that is supposed to do that, but when 'menu' is clicked, the app crashes.
The reason for the crash: 'Tried to pop to a view controller that doesn't exist.'
Here is part of my code:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 0 = menu
if indexPath.row == 0 {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
let _ = navigationController?.popToViewController(viewController!, animated: true)
} else {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}