JeffD JeffD - 5 months ago 24
iOS Question

What is the relationship between a UIPageViewController and a UIPageControl?

So i'm building a simple app where I have three view controllers, and I want to be able to swipe between them (think, the way tinder flows through views from left to right).

The best way to do this appears to be using a UIPageViewController.

But I am getting confused with exactly how the UIPageControl gets involved in this process. I create a UIPageViewController, and can cycle through the three views, but how do I access the specific UIPageControl element that this process uses.

Or perhaps I've confused myself, and a UIPageControl element is not even used. Please help.

I should add, how do I access the UIPageControl element? Do I need to create it? Or is it there by default?

Answer

A UIPageViewController is a container view controller, that is a view controller that can contain other view controllers. These other view controllers are the pages that you want to display, while a UIPageControl is a user interface element that the user can interact with.

In order to support a UIPageViewController you need to implement a the UITableViewDatasource protocol somewhere in your code. This protocol is used to supply the view controller for each page. The UIPageViewController implements a swipe gesture recogniser and the transition between each page.

If you implement the presentationCountForPageViewController and presentationIndexForPageViewController data source methods, then the UIPageViewController will display a UIPageControl (dots) at the top of the screen and also handle the user interacting with this control.

When you use a UIPageControl you can place it anywhere on your view, just like any other control, you need to set properties that indicate the number of pages & the selected page, and you need to implement an action handler to handle user interaction with the control; what you do with the user action is up to you.

Finally, here are a couple of tutorials that demonstrate: