This is an old question and my answer may not even directly address the issue beguiling Tom, but maybe the info will help someone. The UIPageViewController vexed me for a number of days. My mistake was to assume these methods only get called when a page is turned:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
In reality they seem to get called mutiple times during a page turn. I had a currentPage
variable in the master viewcontroller which I incremented/decremented whenever these two methods were called, but it created bizarre behavior similar to what Tom described. I still use those methods (you have to), but do the increment/decrement in a different method. Here's what those methods look like:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (self.currentPage==0) {
return nil;
}
else {
return [[self pageArray] objectAtIndex:self.currentPage-1];
}
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (self.currentPage==self.pageArray.count-1)
{
return nil;
}
else
{
return [[self pageArray] objectAtIndex:self.currentPage+1];
}
}
The increment/decrement of currentPage
occurs in this method:
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (completed)
{
int currentIndex = ((UIViewController *)[self.pageViewController.viewControllers objectAtIndex:0]).view.tag;
self.currentPage = currentIndex;
}
}
To use this method, you have to add both protocols
<UIPageViewControllerDataSource, UIPageViewControllerDelegate>
not just the data source, and set the delegate to self
self.pageViewController.delegate=self;
or the method won't be called. I'm using view tags to determine each page's index, but there are other ways to get this number. See this article for a discussion of ways to get the current page's index:
UIPageViewController: return the current visible view
The UIPageViewController tutorials I found on the web all showed the use of a single viewcontroller that gets reused for each page, which didn't help me because I have a separate viewcontroller for each page.