如何正确替换视图控制器 [英] How to properly replace view controllers
问题描述
我就是不明白我做错了什么.我有 2 个不相关的视图控制器,我在 storyboard
中通过将视图控制器拖到屏幕上来创建它们.每个在故事板中都有其名称标识符,我用它们的名称替换它们.
I just can't understand what am i doing wrong with this. I have 2 unrelated view controllers, which i created in storyboard
by dragging a view controller to screen. each has its name identifier in storyboard and i replace between them by their names.
问题是,在第一次更换后,我收到数十个与现实无关的内存警告,因为我的内存消耗约为 59M 并保持不变.当我从 A 切换到 B 然后再切换回 A 时,有时会由于记忆湖"(59M!!)而崩溃.
Problem is ,that after the first replacement i get tens of memory warnings that are unrelated to reality because my memory consumption is about 59M and stay like that . When i switch from A to B and than back to A, sometimes its crash due to "lake of memory" (59M!! ) .
更换视图控制器时我做错了什么???
What am i doing wrong when replacing view controllers ???
- (IBAction)goMatrix:(id)sender
{
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
mainV.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mainV animated:YES completion:^(void)
{
scroller.delegate=nil;
scroller=nil;
[imagesBuffer removeAllObjects];
[scroller removeFromSuperview];
[stripView removeFromSuperview];
}];
}
我在它们两个中都有一个带有图像的滚动视图,我只是从超级视图中删除了它,我只是不明白我还能做些什么来真正清理东西(尽管内存只是59M)
I have a scrollview in both of them with images, that i just remove from the superview, i just can't understand what else can i do to REALLY clean things up (although memory is just 59M )
- 我唯一拥有高内存的时刻是当我更换它们时,它的 134M 只有 1 秒(!)
推荐答案
如果你的 App 是单视图结构(没有嵌入在 UINavigationController
或 UITabBarController
中),你可以使用:
If your App is a single view structure (without embedded in UINavigationController
or UITabBarController
), you can use:
// App Delegate
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
self.window.rootViewController = mainV;
[self.window makeKeyAndVisible];
stripView = nil;
如果您使用 ARC,旧的视图控制器将自动从内存中删除.
Old view controller will be automatically be removed from memory if you use ARC.
如果你在 View Controller 中编写,你可以使用:
If you are writing in View Controller, you can use:
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = mainV;
[delegate.window makeKeyAndVisible];
stripView = nil;
这篇关于如何正确替换视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!