我想要做的是将所有以前的视图控制器从堆栈中弹出到“菜单”控制器。我有一段代码应该这样做,但是当点击“菜单”时,应用程序崩溃了。
崩溃的原因:“试图弹出一个不存在的视图控制器。”
这是我的代码的一部分:
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)
}
}