Ios 如何正确更换视图控制器
我只是不明白我做错了什么。我有两个不相关的视图控制器,通过将视图控制器拖动到屏幕,我在故事板中创建了它们。在故事板中,每一个都有其名称标识符,我用它们的名称替换它们之间的名称 问题是,在第一次更换后,我收到了数十条与实际情况无关的内存警告,因为我的内存消耗量约为59M,并保持不变。当我从A切换到B,然后再切换回A时,有时它会因为“记忆湖”(59M!!)而崩溃 更换视图控制器时我做错了什么Ios 如何正确更换视图控制器,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,我只是不明白我做错了什么。我有两个不相关的视图控制器,通过将视图控制器拖动到屏幕,我在故事板中创建了它们。在故事板中,每一个都有其名称标识符,我用它们的名称替换它们之间的名称 问题是,在第一次更换后,我收到了数十条与实际情况无关的内存警告,因为我的内存消耗量约为59M,并保持不变。当我从A切换到B,然后再切换回A时,有时它会因为“记忆湖”(59M!!)而崩溃 更换视图控制器时我做错了什么 - (IBAction)goMatrix:(id)sender { UIViewController
- (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];
}];
}
我在这两个视图中都有一个带有图像的滚动视图,我只是从superview中删除了它,我只是不明白我还能做些什么来真正清理东西(尽管内存只有59M)
- 我唯一拥有高内存的时刻是当我更换它们时,它的134M内存只有1秒(!)
UINavigationController
或UITabBarController
),您可以使用:
// App Delegate
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
self.window.rootViewController = mainV;
[self.window makeKeyAndVisible];
stripView = nil;
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = mainV;
[delegate.window makeKeyAndVisible];
stripView = nil;
如果使用ARC,旧的视图控制器将自动从内存中删除
如果要在视图控制器中编写,可以使用:
// App Delegate
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
self.window.rootViewController = mainV;
[self.window makeKeyAndVisible];
stripView = nil;
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = mainV;
[delegate.window makeKeyAndVisible];
stripView = nil;
我不知道我是否理解你所说的“可以使用”是什么意思,我该如何处理他的代码?我必须能够从B替换到A,而不是从A替换到B,我该怎么做,我当前的代码有什么问题?我不认为在情节提要中,你需要使用setRootViewController或Makerkeyandvisible,因为情节提要可以为你做到这一点。仅在appdelegate中定义了一个窗口&在ApplicationIDFinishLaunching中返回YES。这是不正确的。你仍然可以在故事板上使用这些。为什么59M会有记忆警告?@Raptor你的答案很好,非常感谢!让我感到奇怪的是,如果你说苹果拥有高记忆的瞬间,为什么它会有这样的转变。