从UIPageViewController卸载viewControllers

问题描述:

我有一个UIPageViewController,我添加了其他viewControllers。那些ViewControllers在数组 viewControllersArray = [[NSMutableArray alloc] init]; 我在这个数组上添加viewControllers,如下所示:

I got a UIPageViewController where I add other viewControllers. Those ViewControllers are in the array viewControllersArray = [[NSMutableArray alloc] init]; I add viewControllers on this array like this:

        [viewControllersArray addObject: infoViewController];

在我的阵列上添加viewControllers之后:

After the viewControllers are added on my array:

NSArray *initialViewControllers = [[NSArray alloc] initWithObjects:[viewControllersArray objectAtIndex:0], nil];
[self setViewControllers:initialViewControllers
               direction:UIPageViewControllerNavigationDirectionForward
                animated:YES
              completion:^(BOOL finished){
                  NSLog(@"call back success");}];

以上代码全部来自我的UIPageViewController的viewDidLoad。

This above code is all done in the viewDidLoad from my UIPageViewController.

为了加载这个数组,我从< UIPageViewControllerDataSource>
获得了这些函数,我这样使用:

For loading this array I got those functions form <UIPageViewControllerDataSource> which I use like this:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if ([viewControllersArray containsObject:viewController]) {
        NSInteger index = [viewControllersArray indexOfObject:viewController];
        if (index < [viewControllersArray count] && index > 0) {
            return [viewControllersArray objectAtIndex:(index - 1)];
        }
    }
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if ([viewControllersArray containsObject:viewController]) {
        NSInteger index = [viewControllersArray indexOfObject:viewController];
        if (index < [viewControllersArray count] - 1) {
            return [viewControllersArray objectAtIndex:(index + 1)];
        }
    }
    return nil;
}

现在问题是,当你滑动时,下一个viewcontrollers会得到负载等,但他们没有卸载。所以我正在寻找一种方法来卸载已经传递的视图控制器以节省内存,当你向后滑动时它们将被重新加载。

Now what the problem is, is that when you swipe, the next viewcontrollers gets load, etc. But they don't get unloaded. So I'm searching for a way to unload the viewcontrollers that you already passed to save memory, and when you swipe back they will get reloaded.

你的问题是你在 viewControllersArray 中坚持你的视图控制器。该数组对您添加到其中的每个对象都有一个强引用。保存内存的最简单方法是在 pageViewController:viewControllerBeforeViewController: pageViewController:viewControllerAfterViewController:数据源方法。这样,只有页面视图控制器才会对视图控制器有强烈的引用,并且一旦它移出屏幕就会释放(dealloc)。

your problem is that you hold on to your view controllers in the viewControllersArray. The array holds a strong reference to each object that you add to it. The easiest way to save memory is to just create the requested view controller on the fly in both the pageViewController:viewControllerBeforeViewController: and the pageViewController:viewControllerAfterViewController: data source methods. That way, only the page view controller will have a strong reference to the view controller and will release (dealloc) it once it is moved off-screen.

如果它也是在运行中创建它们的成本很高,您可能会考虑在阵列中保留3个视图控制器:当前显示的控制器,以及左侧和右侧的一个。

If it's too expensive to create them on-the-fly, you might consider just keeping 3 view controllers in the array: the one currently displayed and the one to the immediate left and the immediate right.

示例:

- (UIViewController *)pageViewController:(UIPageViewController *)pvc
      viewControllerBeforeViewController:(MyGreatViewController *)vc
{
    NSUInteger index = vc.position - 1; //custom property on your VC
    if (index > 0) {
      return [[MyGreatViewController alloc] initWithPosition:index];
    }

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc
       viewControllerBeforeViewController:(MyGreatViewController *)vc
{
    NSUInteger index = vc.position + 1; //custom property on your VC
    if (index <= MAX_POSITION) {
      return [[MyGreatViewController alloc] initWithPosition:index];
    }

    return nil;
}

享受!