4

I'm using Xcode 8's default Page-based application, and I'm stuck trying to jump to a particular page (as opposed to swiping left and right to turn). I have found similar questions on StackOverflow, but the answers mostly suggested using this method:

 setViewControllers:direction:animated:completion

I don't need to change the number of pages to be displayed, so can I avoid using setViewControllers?

After reading through Xcode's page-based application template, I think this function may work:

func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> DataViewController?

However, I don't know where to get the parameter storyboard: UIStoryboard, since ModelController (the controller that serves as UIPageViewControllerDataSource) isn't part of the storyboard.

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
swiftlycoderr
  • 43
  • 1
  • 5

1 Answers1

3

The view controllers passed to this method are those that will be visible after the animation has completed. Use a data source to provide additional view controllers to which users navigate.

When defining a page view controller interface, you can provide the content view controllers one at a time (or two at a time, depending upon the spine position and double-sided state) or as-needed using a data source. When providing content view controllers one at a time, you use the setViewControllers(_:direction:animated:completion:) method to set the current content view controllers. To support gesture-based navigation, you must provide your view controllers using a data source object.

The data source for a page view controller is responsible for providing the content view controllers on demand and must conform to the UIPageViewControllerDataSource protocol.

The delegate object—an object that conforms to the UIPageViewControllerDelegate protocol—provides some appearance-related information and receives notifications about gesture-initiated transitions.

setViewControllers([<#Your ViewControllers#>], direction: .forward, animated: true, completion: nil)

More Info

Apple Docs

Stackoverflow

Harshal Valanda
  • 5,331
  • 26
  • 63
  • This is so helpful! Can you check if i'm understanding this correctly: I can provide viewControllers one or two at a time when I need to jump to a page, and still use the datasource object to support gesture navigation? – swiftlycoderr Jul 14 '17 at 06:19
  • yup .. setViewControllers:direction:animated:completion only sets the visible view controllers – Harshal Valanda Jul 14 '17 at 06:25
  • It makes sense to me now, and i should probably read apple's official doc thoroughly before posting here in the future. thanks! – swiftlycoderr Jul 14 '17 at 06:30