从堆栈中移除 ViewController

remove ViewControllers from stack

我有一个viewController(暂且称它为VC1),它在应用程序启动时显示,它有一个打开模态VC的菜单按钮,在模态VC有三个按钮打开VC1、VC2、VC3(在VC2、VC3也有这个菜单按钮)

我 运行 我的应用程序(在 VC1),我按下菜单按钮(模态VC),打开 VC1。

原来我现在开了两个VC1?因为我每次按VC1,都会增加1-2mb的内存。我怎样才能将它们从内存中删除?

必须在 select 控制器菜单(模态VC)之后以某种方式删除之前的所有内容..如何?

从VC1、VC2、VC3 上的菜单过渡: UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: VC1]; [self presentViewController: navController animated: YES completion: nil];

如果我总是按下菜单按钮 select VC2(10 次会这样做)应用程序会变慢

我在 select VC 之后在菜单中尝试过此操作 (modalVC)

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeAllObjects];
self.navigationController.viewControllers = allViewControllers;

您的菜单实施存在设计缺陷。每当用户点击按钮时,您都会为每个 ViewController 创建新实例,而不是使用您之前已经创建的实例。应该使用 ViewController 遏制来实现这样的菜单。这样,容器 ViewController 将在需要时创建 VC1 VC2 和 VC3,如果它们已经存在则不会重新创建它们。

要了解您将如何做到这一点,只需阅读 Apple docs

中名为 "Implementing a Custom Container View Controller" 的部分