Preloading pages of UIPageViewController

Eric picture Eric · Jul 26, 2012 · Viewed 10.5k times · Source

I can't find a good solution to having the UIPageViewController preload the surrounding ViewControllers.

Right now what happens is when the user starts to turn the page, nothing happens, the code calls

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController

and once its loaded thennnn it shows the animation. This takes too long and is not smooth.

Anyone have a solution?

Answer

David H picture David H · Jul 26, 2012

Create a mutable container and when you show the first view, create the two viewController that would be needed if the user pages, once you have the viewController object ask it for its view (to get it to load the nib and call the controller's "viewDidLoad:" method. You need to figure out a system on identifying the viewControlers so you can retrieve the viewController you need). You might be able to do the heavy lifting here in a block on a dispatch_queue.

From then on, when the user pages, you look first in the container for the viewController, and if not found, you have to do it in real time.

You probably want to use a dispatch_group so that you can wait on it for pending blocks to finish before paging.

Everytime the user pages, you will look and see if the pages surrounding that page are in the container or not. You could also pre-fetch more viewControllers - like two forward two reverse at each page shown.