I’m using a scrollview and a pagecontrol to render several images, very similar to the built-in photo app. I’m using this code to setup the controllers when the view loads:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [photos count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollArea.pagingEnabled = YES;
scrollArea.contentSize = CGSizeMake(newW * [photos count], newH);
scrollArea.showsHorizontalScrollIndicator = NO;
scrollArea.showsVerticalScrollIndicator = NO;
scrollArea.scrollsToTop = NO;
scrollArea.delegate = self;
pageControl.numberOfPages = [photos count];
newW and newH are just two variables I’m using to keep track of the size when the orientation changes.
My loadScrollViewWithPage method:
- (void) loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [photos count]) return;
PhotoViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PhotoViewController alloc] init];
controller.imgPath = (NSString *)[photos objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollArea.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollArea addSubview:controller.view];
}
}
And here is my issue. I need to unload controllers/views when I need it. I thought I could use something like this:
for (unsigned i = 0; i < [photos count]; i++) {
[self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
I want to do it, so load completely new views into the scrollview.
I also tried something liek this with no luck:
for(UIView *subview in [self.scrollArea subviews]) {
[subview removeFromSuperview];
}
Any ideas?
So you want to make sure the subviews of
scrollViewstay in sync with the items inself.viewControllersas you update the latter? The easiest thing is to do something like this whenever you replace one of the items: