我有一个应用程序在滚动视图中加载了许多视图控制器,具体取决于用户在表格视图中拥有的对象数量。因此,当我在 tableview 和滚动视图之间切换时,滚动视图中的视图控制器数量会根据用户在 tableview 中拥有的对象数量而变化。
当然,经过一些修改,我使用 Apple 的 PageControl 示例代码中的代码来构建包含许多视图控制器的滚动视图。
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
MainViewController *countdownController = [viewControllers objectAtIndex:page];
if ((NSNull *)countdownController == [NSNull null])
{
id occasion = [eventsArray objectAtIndex:page];
countdownController = [[MainViewController alloc] initWithPageNumber:page];
[countdownController setOccasion:occasion];
[viewControllers replaceObjectAtIndex:page withObject:countdownController];
[countdownController release];
}
// add the controller's view to the scroll view
if (nil == countdownController.view.superview)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
countdownController.view.frame = frame;
[scrollView addSubview:countdownController.view];
}
}
问题是当我在表格视图和滚动视图(根据 Instruments)之间切换时,活动视图控制器(此处为 MainViewController)的数量不断增加,即使我没有添加任何会导致内存问题的新对象。
我在滚动视图的 viewWillDisappear 中尝试了很多东西,例如:
- (void) viewWillDisappear:(BOOL)animated
{
//test unloading all views
//Remove all subviews
[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
//[[scrollView subviews] makeObjectsPerformSelector:@selector(release)];
//[viewControllers removeAllObjects];
for (unsigned m = 0; m < [viewControllers count]; m++)
{
//[[viewControllers objectAtIndex:m] makeObjectsPerformSelector:@selector(release)];
[viewControllers removeObjectAtIndex:m];
}
}
但它没有用。这是该应用程序如何工作的记录 youtube.com/watch?v=5W8v_smZSog
这是滚动视图的 viewWillAppear 方法:
- (void)viewWillAppear:(BOOL)animated
{
eventsArray = [[NSMutableArray alloc] init];
kNumberOfPages = [self.dataModel occasionCount];
//update the eventsArray from the dataModel
//Fill in the events Array with occasions form the data model
for (unsigned r = 0; r < kNumberOfPages; r++)
{
Occasion* occasion = [self.dataModel occasionAtIndex:r];
[eventsArray insertObject:occasion atIndex:r];
}
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = currentPage;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
更新:仪器视频录制http://www.youtube.com/watch?v=u1Rd2clvMQE&feature=youtube_gdata_player
以及显示负责呼叫者的屏幕截图:
谢谢你。