开发者

loadView doesn't load my views properly after memory warning

开发者 https://www.devze.com 2023-01-03 20:42 出处:网络
I set all the subviews that I created in loadView to nil in viewDidUnload. I simulate a memory warning in simulator, and my offscreen views and their subviews get removed via viewDidUnload. However,

I set all the subviews that I created in loadView to nil in viewDidUnload.

I simulate a memory warning in simulator, and my offscreen views and their subviews get removed via viewDidUnload. However, when loadView gets called again as these offscreen views come into view, my subviews don't get recreated properly the second time. For example, my labels and table views are empty, that I created in loadView:

CGRect frame = CGRectMake(0, 0, 400, 600);
UIView *theView = [[UIView alloc] initWithFrame:frame];
self.view = theView;
[theView release];

int w = frame.size.width;
int h = frame.size.height;
CGRect tblFrame = CGRectMake(0, h/10, w, h*7/10);
UITableView *tblvw = [[UITableView alloc] initWithFrame:tblFrame style:UITableViewSty开发者_如何转开发lePlain];
tblvw.delegate = self;
tblvw.dataSource = self;
self.resourcesTblVw = tblvw;
[tblvw release];
[self.view addSubview:resourcesTblVw];


CGRect lblFrame = CGRectMake(0, 0, w, 36);
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
lbl.font = [UIFont boldSystemFontOfSize:20];
lbl.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0];
lbl.text = name;
self.nameLabel = lbl;
[lbl release];
[self.view addSubview:nameLabel];

Ideas?


first guess would be that this happens because the second time, the code that gets executed to load the data in the viewdidload is not there in the loadview method. so copy the code from where you load the data into the tables and so on into the loadView method...

another thing you could do... the preferred option is to create a method such as

Example:

-(void)loadData {
//enter code here to load all the tables and so on.

}

Then call this method when the view loads... that should work and call this method in the loadView method.... so that everything gets loaded once youve set the views again after the memory warning.

PK

0

精彩评论

暂无评论...
验证码 换一张
取 消