gpt4 book ai didi

ios - 内存管理 - 如何在不创建新实例的情况下显示已实例化的 ViewController

转载 作者:行者123 更新时间:2023-11-29 03:45:49 25 4
gpt4 key购买 nike

我遇到了严重的内存管理问题。少量使用该程序后,它会因内存不足而崩溃。我终于找到了原因,每次我创建一个新的 ViewController 而不是访问实例时,我都是在创建一个新实例。

因此应用程序加载并实例化 FirstViewController。您单击一个实例化 FilterViewController 的按钮。从这里返回到 FirstViewController 时,我将创建一个新的实例,如下所示:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName
:@"MainStoryboard" bundle:nil];

FirstViewController *fvc = [storyboard
instantiateViewControllerWithIdentifier:@"FirstViewController"];

fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

并重复该过程。有什么方法可以在不重新实例化 View Controller 的情况下呈现它?我即将提交应用程序(希望明天),所以我需要尝试对此进行排序。谢谢!

这是 ViewController 的演示。

[self presentViewController:fvc animated:YES completion:nil];

FirstViewController 呈现 FilterViewController

- (IBAction)searchOptions:(id)sender {
FilterViewController *ctrl = [[FilterViewController alloc] init];
[UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:nil];

self.filterViewController = ctrl;

[self.navigationController pushViewController:self.filterViewController animated:NO];

}

最佳答案

如果您使用的是 presentViewController,则可以通过调用 [self DismissViewControllerAnimated:YES]; 返回到上一个 View 。您可以在当前创建新 Controller 的方法中执行此操作。

如果您要插入导航 Controller ,您将从导航 Controller 中弹出:[self.navigationController popViewControllerAnimated:YES];


根据您的上次更新,您似乎没有导航 Controller ,而您只是将 View 添加为 subview 并存储过滤器 View Controller 。这确实使生活变得更加复杂,删除它的正确方法是设置委托(delegate)关系,以便过滤器 View Controller 在完成时回调到第一个 View Controller ,然后第一个 Controller 转换 View ,nil 是引用。

如果可以,请更改为正确使用导航 Controller 。您已经有了一半的代码,但第一个 View Controller 似乎不在导航 Controller 中。如果您使用导航 Controller ,生活将会变得轻松......

关于ios - 内存管理 - 如何在不创建新实例的情况下显示已实例化的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17777457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com