How to properly replace view controllers

送分小仙女□ 提交于 2019-12-25 05:07:39

问题


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.

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];


     }];

}

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 )

  • the only moment i have high memory is when i replace them, and its 134M for only 1 second(!)

回答1:


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;

Old view controller will be automatically be removed from memory if you use ARC.

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;


来源:https://stackoverflow.com/questions/23778437/how-to-properly-replace-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!