且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

释放滚动视图内的多个视图控制器的内存

更新时间:2022-12-26 19:07:16

如果您不想使用UIPageViewController(请阅读我的其他答案),这是给您的.

This is for you if you don't want to use UIPageViewController (read my other answer).

该示例项目被设计用于恒定数量的页面(kNumberOfPages). scrollview内容的大小和视图控制器数组的大小取决于页面数.示例代码在awakeFromNib中进行了设置,该过程仅被调用一次.

The sample project is designed for a constant number of pages (kNumberOfPages). The scrollview content size and the size of the view controller array depends on the number of pages. The sample code set this up in awakeFromNib, which is called only once.

因此,为了使其动态化,您可以在页数更改时重新创建整个ContentController.您只需要为页数添加一个属性即可.

So in order to make this dynamic you could recreate the whole ContentController when the number of pages changes. You just need to add a property for the number of pages.

另一种选择是在页数更改时重置滚动视图和视图控制器阵列.

The other option would be to reset the scrollview and view controller array when the number of pages changes.

我假设您已经为事件定义了一个属性:

I'm assuming you have defined a property for the events:

@property(nonatomic,retain) NSArray* eventsArray;

然后您可以添加如下的setter方法:

You could then add a setter method like this:

-(void)setEventsArray:(NSArray *)eventsArray
{
    if (eventsArray != _eventsArray) {
        [_eventsArray release];
        _eventsArray = [eventsArray retain];
        NSUInteger eventCount = [eventsArray count];
        //reset scrollview contentSize
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * eventCount, scrollView.frame.size.height);

        // reset content offset to zero
        scrollView.contentOffset = CGPointZero;

        //remove all subviews
        [[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

        pageControl.numberOfPages = eventCount;

        // reset viewcontroller array
        NSMutableArray *controllers = [[NSMutableArray alloc] init];
        for (unsigned i = 0; i < eventCount; i++)
        {
            [controllers addObject:[NSNull null]];
        }
        self.viewControllers = controllers;
        [controllers release];

        [self loadScrollViewWithPage:0];
        [self loadScrollViewWithPage:1];
    }
}

您在用户切换到滚动视图时从表视图控制器调用此方法.

You call this method from the table view controller at the time when the user switches to the scroll view.