How to get Data to UIPageViewController Pages

Hi,


I'm working on a project for my university and currently my problem is, that I am not able to get data to the Pages which get called by the UIPageViewController. I've seen some disscussions about it but none of the answers do really cover this topic or show working solutions.


I can get the data to my UIPageViewController class but how do i pass it on to every page attached to it?



Thank you!

Accepted Reply

I tested this, seems to work OK.


In PageViewController, declare the var you want to pass to children (here an Int, but could be anything, as the instance of a self written data class)


class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource  {

    var pageControl = UIPageControl()
    var testVar: Int = 0


In the childView, get this data :


    override func viewDidLoad() {
        super.viewDidLoad()
        let passedData = (self.parent as! PageViewController).testVar
        print(passedData)
  • Works Great!

  • What do you use, if you don't pass to children?

    self.

Add a Comment

Replies

Data should usually be in each page, not in the pageController. PageController "just" holds the dots, keep track of active page, get the array of UIViewControllers…


What data do you want here ?


May look here for a complete example

h ttp://www.seemuapps.com/page-view-controller-tutorial-with-page-dots

Makes sense, that the data should only be on the pages. I'm currently passing an instance of a self written data class with the help of segues to all the needed destinations. But there is no segue called when opening the Pages through a PageViewController so I haven't found a way to get data to them.

I tested this, seems to work OK.


In PageViewController, declare the var you want to pass to children (here an Int, but could be anything, as the instance of a self written data class)


class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource  {

    var pageControl = UIPageControl()
    var testVar: Int = 0


In the childView, get this data :


    override func viewDidLoad() {
        super.viewDidLoad()
        let passedData = (self.parent as! PageViewController).testVar
        print(passedData)
  • Works Great!

  • What do you use, if you don't pass to children?

    self.

Add a Comment