如何从导航堆栈中弹出上一个视图控制器?

时间:2017-04-27 17:59:41

标签: ios swift memory-leaks uinavigationcontroller viewcontroller

我使用导航控制器从视图推送到视图,但我不会弹出我推送的任何视图。因此,当我加载视图时,我想从堆栈中弹出前一个视图。

推送视图的代码:

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帮助我完成了对该行代码的正确使用和放置。

2 个答案:

答案 0 :(得分:4)

它真的更简单:

self.navigationController?.popViewController(animated: true)

答案 1 :(得分:1)

这对我有用:

var navigationArray = self.navigationController?.viewControllers

navigationArray.remove(at: 0)

self.navigationController?.viewControllers = navigationArray