如何从导航堆栈中弹出前一个视图控制器?
How to pop the previous view controller from a navigation stack?
我使用导航控制器从一个视图推送到另一个视图,但我不弹出任何我推送的视图。因此,当我加载一个视图时,我想从堆栈中弹出前一个视图。
浏览量推送代码:
var identities = [String]()
identities = ["A", "B", "C", "D", "E"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
弹出视图的测试代码(不是我想要的):
self.navigationController?.viewControllers.remove(at: 0)
如有任何帮助,我们将不胜感激。
已解决:
@DonMag 帮助我了解了该行代码的正确使用和放置。
这对我有用:
var navigationArray = self.navigationController?.viewControllers
navigationArray.remove(at: 0)
self.navigationController?.viewControllers = navigationArray
真的简单多了:
self.navigationController?.popViewController(animated: true)
我使用导航控制器从一个视图推送到另一个视图,但我不弹出任何我推送的视图。因此,当我加载一个视图时,我想从堆栈中弹出前一个视图。
浏览量推送代码:
var identities = [String]()
identities = ["A", "B", "C", "D", "E"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
弹出视图的测试代码(不是我想要的):
self.navigationController?.viewControllers.remove(at: 0)
如有任何帮助,我们将不胜感激。
已解决:
@DonMag 帮助我了解了该行代码的正确使用和放置。
这对我有用:
var navigationArray = self.navigationController?.viewControllers
navigationArray.remove(at: 0)
self.navigationController?.viewControllers = navigationArray
真的简单多了:
self.navigationController?.popViewController(animated: true)