Casey Perkins
Casey Perkins

Reputation: 1932

where does UIPageViewController cache the previous and next controllers?

When UIPageViewController calls the datasource with the viewControllerBeforeViewController and viewControllerAfterViewController methods, it is obtaining the view controller that will be displayed when the user swipes again. Is there a writable property in which it keeps this data until it needs to use it? The reason I ask is that I want the user to be able to swipe to go forward or backward, or simply tap to advance to the next slide. However, if I tap to advance from the first slide to the second (advancing using the setViewControllers method), I cannot swipe backwards; there is no back controller to go back to. This affects only the second slide. So I need to be able to set the previous controller programmatically.

Any help would be greatly appreciated.

Thanks.

Upvotes: 8

Views: 2782

Answers (4)

Matty Cross
Matty Cross

Reputation: 62

I was able to resolve my consistency problem thanks to Kelin's answer. Every time I want to change the controllers in the UIPageViewController and make sure none of them are cached, I call this before the setViewControllers method:

for child in children {
    child.willMove(toParent: nil)
    child.view.removeFromSuperview()
    child.removeFromParent()
}

Upvotes: 0

kelin
kelin

Reputation: 11991

I had the similar issue and discovered, that UIPageViewController store its cached View Controller pages in the childViewControllers array.

Upvotes: 9

Pochi
Pochi

Reputation: 13459

As described in Kelin's answer, the children array contains the "loaded" view controllers. This information can also be used for propagating changes among loaded view controllers. (A common scenario would be ending the refresh of a refresh control.)

In Swift you can do something like this:

for vc in children {
    if let tableVC = vc as? UITableViewController {
         tableVC.refreshControl?.endRefreshing()
    }
}

(Where self is of type UIPageViewController)

Upvotes: 0

Yariv Nissim
Yariv Nissim

Reputation: 13343

There is no access to the cached view controllers. You can maybe keep a pointer when you implement the Data Source methods to enable swiping:

– pageViewController:viewControllerBeforeViewController:
– pageViewController:viewControllerAfterViewController:

Upvotes: 0

Related Questions