0

As is typical, I build an array of UIViewControllers and then display them through a UIPageViewController. In my managing controller class, I added a method to go directly to a page. But after doing so, the UIPageViewController shows the wrong adjacent page.

I'm pretty sure that this is because it makes a bad assumption: It starts on a page; and if you push a new controller onto it and say the direction of travel is to the right, UIPageViewController assumes that the previous controller is the one directly to the left.

Of course that fails if you start on page 0 and push the controller for page 2 onto the PageViewController. If you swipe back to the left, you see page 0 again, because UIPageViewController seems to have cached the original view as the one to the left. It never calls its delegate to "get previous view controller" because it thinks it already has it. But it should call and get page 1.

enter image description here This is my guess, anyway, based on the behavior and the fact that my delegate's "get previous controller" is never called if I swipe left after a jump to the right. So do I have to do a sequence of pushes to get from, say page 0 to page 2?

Anybody have a better workaround or insight? Thanks!

Oscar
  • 2,039
  • 2
  • 29
  • 39
  • You have implemented the `viewControllerBefore` and `viewControllerAfter` data source methods and assigned the data source of the page view controller? Can you show the code for those methods. – Paulw11 May 23 '19 at 00:35
  • Thanks for your response. I have implemented them and they work fine in all other circumstances. It turns out that my theory was correct. See https://stackoverflow.com/questions/25016453/uipageviewcontroller-viewcontrollerafterviewcontroller-not-called-after-once-ret – Oscar May 23 '19 at 00:44
  • That describes a different issue - You didn't say that you had previously returned `nil` for the view controller at a specific location. I just modified the sample paged app from Xcode to jump to "December" after 10 seconds and it scrolled back to "November" correctly. – Paulw11 May 23 '19 at 01:13
  • It didn't previously return nil. However, the behavior described and the explanation is exactly in line with my analysis of what's happening in my app... and the workaround also applies. – Oscar May 23 '19 at 01:42
  • I suspect that you still have an error in your code; as I said the sample app from the Xcode template behaves correctly without any need for any work arounds. – Paulw11 May 23 '19 at 01:49
  • I looked at the sample app, and it differs significantly. It creates every view controller on the fly as the user navigates back and forth, saving none of them. If you can find an example that iterates through a static collection of view controllers, it will be a much more valid comparison. – Oscar May 23 '19 at 02:19
  • It was fairly trivial to convert the sample to use a fixed array of view controllers. It still works correctly. – Paulw11 May 23 '19 at 04:05
  • Yet another example: https://stackoverflow.com/questions/36853129/uipageviewcontroller-can-return-the-wrong-index – Oscar Aug 20 '19 at 06:28

0 Answers0