Developer tip: Taming UIPageViewController to do paging

I am coming off of several days of head-scratching as I have been trying to understand how UIPageViewController works. There is a lot of documentation to read and even with a good tutorial I still had questions. Hopefully this helps you:

1. Forget Interface Builder

Seriously. IB has given me more headaches. Yes, there’s a component you can drag and drop but it never worked great for me. Just make a plain view as the container that will hold your UIPageViewController. Even if you use Xcode’s new project Page-Based Application template you’ll end up with the same thing.

2. You want to implement the <UIPageViewControllerDataSource> protocol on your UIViewController

Keep things easy to begin with: whatever UIViewController that is serving up your UIView, make it the data source too. You will have two methods to implement where they will get the current UIViewController and be expected to return new UIViewControllers for the page before or after.

Note that the gist also has a factory method to return the UIViewController for the page number. In this case, all pages are just instances of SomePageViewController. You can imagine that each page might be a different view controller.

3. Don’t worry about navigation dots unless you want to.

If you want those dots on the bottom of the screen that show the user which page they are looking at, you’ll need to implement something like this:

3b. If you do implement the dots, presentationIndexForPageViewController: may only be called once

I had thought that every time you navigate this method would get called. Not necessarily true: it and the count method get called during some setup phase and don’t need to be called again. If you want to keep updating the number of dots or do some cute navigation trick where the current dot doesn’t map exactly to a specific page, good luck.

4. Alloc/init the child UIPageViewController

In your viewDidLoad: of the UIViewController mentioned in Step 1, do something like this:

5. There is no manual previous or next page … but you can reset the view controllers array and fake it with animation

UIPageViewController seems like it is overly simplistic. There is apparently no easy way to navigate prev/next. But what you can do is reset that view controllers array and indicate you want it to animate. So, let’s say you want to go to the previous page, this is a possibility:

That’s not great but it works. For instance, let’s say you wanted to go to the first page, if you followed a similar technique then:

To the user it would look like the page coming in immediately to the left is the first page. They would not see a flurry of pages fly by as it seeked to the first page.