简体   繁体   中英

UITabBarController deallocating views after memory warning

I'm creating an app using a UITabBarController as the main form of navigation, having never done this before I've ran into a little bit of an issue.

On two of the four tabs, the view controllers held drill down using a standard navigation controller and browse through categories and then into items, the categories and items have quite a few images.

After a few minutes of using my app (down to the item stage where a scrollview of 4 320x460 images is used) i get..

Received memory warning. Level=1

This then deallocate's all of the view's currently held in my UITabBarController, and when I click the tabs, i am presented with only white screens.

When using images, I am trying to use..

UIImageView *imageView = [[[UIImageView alloc] init] autorelease];
[imageView setImage:[UIImage imageNamed:@"image"]];
[self.view addSubview:imageView];

..as much as possible, as far as i'm aware, that is 'memory managed', and should dealloc correctly.

The memory issue I believe is merely being caused by 4 large images being loaded into memory, this is a requirement of my app. The tab's dissapearing isn't :/

Any ideas/guidance?

This is by design. When your app receives a memory warning, all view controllers that are not currently visible will unload their views. You have to be prepared for that. When the user reopens a tab that was unloaded, the view controller reloads the view and calls -viewDidLoad again where you should do your view setup.

Ole gave a decent explanation. One thing though, if your off-screen view controller's view gets unloaded, you should setup it again in the -loadView . This method gets called to set up your view hierarchy, then -viewDidLoad gets called, to signal that the view is ready. If you implement it in -viewDidLoad , you would have an empty view loaded (UIViewController -loadView ), then replaced (with your -viewDidLoad code), causing a flicker.

An even more elaborate explanation about loading your views is available here: http://myok12.wordpress.com/2010/11/30/custom-uiviewcontrollers-their-views-and-their-memory-management/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM