I have a problem with memory management. A have a scrollview and every page in it loads from array of view controllers.
I load scrolview page calling the folowing method:
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
BancaTableViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller=[[BancaTableViewController alloc] initWithPageNumber:page];
controller.banks=banks;
[controllersetDelegate:self];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
This is my unload view controller method which unloads all controllers except the controller of curent page but it doesn’t seem to work because the memory keep increasing.
- (void)unloadScrollViewWithPage:(int)page {
for (unsigned i = 0; i < kNumberOfPages; i++) {
if(i!=page){
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
How to write the unloadviewcontroller correctly?
You’re adding a hard pointer to scrollView, but you never remove the object. (i.e. you keep adding subViews to scrollView and never remove them.
Try this…
Add a tag to your controller view when you add them to the scrollView, then remove it before you load a new controller. Check syntax – from memory – not tested