3

I have a ViewController (let's call it MainViewController) that contains a ContainerView. The ContainerView contains only one child - a PageViewController. The PageViewController pages through 4 different ViewControllers (let's call them Red, Blue, Green, and Orange ViewControllers). How would I access the various colored child ViewControllers from the MainViewController (each one contains a UITableView and I'd like to pass the data for those tableviews down from the MainActivity so that I don't have to make separate database calls to get the data from each one of the colored pages)?

Note: I know how to access a ViewController inside a ContainerView using this method: Access Container View Controller from Parent iOS . But that would only get me to the PageViewController - I need to go a level deeper than that. An answer in Swift would be greatly appreciated.

Here's a screenshot to illustrate. enter image description here

And as a follow-on, how would I access the MainViewController from the 4 colored "grand-children" ViewControllers (correct terminology?)?

Community
  • 1
  • 1
A. Vin
  • 865
  • 1
  • 6
  • 19
  • Check out this tutorial https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/ – Pushpa Y May 10 '16 at 05:39
  • I actually used that tutorial to learn PageViewControllers. But it doesn't explain in that tutorial how to access the children of the PageViewController. – A. Vin May 10 '16 at 05:47

2 Answers2

4

As the previous answer pointed, this is a valid way to view the children:

if let myViewControllers = myPageViewController.viewControllers as? [UIViewController] {
    for viewController in myViewControllers {
        // Do something with the viewController
    }
}

But it will only show the one child that is currently in view. If you want to access all the child ViewControllers, including the ones that aren't visible, when you instantiate and add them to the ContainerView/PageViewController, save a reference to them. You can then use that reference to access any function/data inside of each child ViewController.

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
drlff
  • 256
  • 3
  • 12
0

On UIPageViewController, you have a property called viewControllers. It gives you The view controllers displayed by the page view controller.. It's an array, because an UIPageViewController could display multiple pages at the same time. But if you have only one page at the same time, the array will contain only this page.

Here is an example:

if let myViewControllers = myPageViewController.viewControllers as? [UIViewController] {
    for viewController in myViewControllers {
        // Do something with the viewController
    }
}
Julien Quere
  • 2,407
  • 16
  • 21
  • Ok, that makes sense. So I assume I would first gai access to the PageViewController via the prepareForSegue function in the MainViewController, then I would use that reference to PageViewController to get the array of child view controllers? I will give it a shot first thing tomorrow morning. – A. Vin May 10 '16 at 06:07
  • Julien, myPageViewController.viewControllers is returning an array of size 1, despite the fact that there are clearly multiple ViewControllers as children (I'm swiping through them as I type this...). Any advice on this? – A. Vin May 10 '16 at 16:32
  • Yes, that's right. It gives you an array of the *visibles* viewcontrollers (in your case: 1). If you want to find all the viewcontrollers (even those who are not displayed), you have to request your data source yourself. – Julien Quere May 11 '16 at 05:04
  • Could you explain what you mean by "request your data source yourself"? – A. Vin May 11 '16 at 16:01