如何正确replace视图控制器

我只是不明白我在做什么错了。 我有2个不相关的视图控制器,我通过拖动视图控制器到storyboard创build屏幕。 每个在故事板中都有它的名字标识符,我用它们的名字在它们之间进行replace。

问题是,在第一次更换后,我得到了几十条与现实无关的内存警告,因为我的内存消耗约为59M,并保持这样的状态。 当我从A切换到B并且回到A时,有时它会因“记忆湖”(59M !!)而崩溃。

我更换视图控制器时做错了什么?

 - (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)

  • 唯一一次我有很高的记忆力的时候,我换了他们,而它的134M只有1秒(!)

如果您的应用程序是单一视图结构(不embeddedUINavigationControllerUITabBarController ),则可以使用:

 // App Delegate UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"]; self.window.rootViewController = mainV; [self.window makeKeyAndVisible]; stripView = nil; 

如果使用ARC,则旧视图控制器将自动从内存中移除。

如果您在View Controller中编写,则可以使用:

 UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"]; AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; delegate.window.rootViewController = mainV; [delegate.window makeKeyAndVisible]; stripView = nil;