Reputation: 513
I have two view controllers inside a UIPageViewController
. I need to get all view controllers inside the UIPageViewController
to call methods inside them, but it always returns a single page only (either the first page or the second page).
The code is below for convenience:
if let viewControllers: [UIViewController] = self.viewControllers {
print(viewControllers)
for controller in viewControllers {
if let firstController: FirstViewController = controller as? FirstViewController {
//Call something
}
if let secondController: SecondViewController = controller as? SecondViewController {
//Call something
}
}
}
In fact, it returns only the visible view controller inside the page controller.
Upvotes: 1
Views: 615
Reputation: 58049
This is intended behaviour.
In a simple UIPageViewController
, this array will only return the currently visible view controller(s), which is usually just one view controller.
Keep in mind that UIPageViewController
heavily caches and reuses controllers, just like a UITableViewController
would. You may want to reflect changes in another way.
Upvotes: 2