UIPageViewControllerDataSource calls viewControllerAfterViewController twice the first time running

I know this might be a duplicate but I tried other solutions found online and none of them seem to fit my issue.

I have a pageViewController that display’s 4 UIViewControllers.
At first scroll the pageController swipes to the third View controller but shows the second ViewController. I know that because i print the ViewDidLoad function.
I really need the viewdidload function to happen on the viewcontroller that appears on screen.

Here is my code:

_contentPageRestorationIDs = [[NSArray alloc]initWithObjects:@"PRPeripheralsOriantaionVC",@"PRSessionAnalysisDataVC", @"PRNavigationVC",@"PRVideoVC", nil];


- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return self.contentPageRestorationIDs.count;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
        NSString *vcRestorationID = viewController.restorationIdentifier;
    NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];
    NSLog(@"viewControllerBeforeViewController %@",vcRestorationID);

    if (index == 0)
    {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index ];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSString *vcRestorationID = viewController.restorationIdentifier;
NSLog(@"viewControllerAfterViewController %@",vcRestorationID);

    NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];
    index ++;
    if (index == self.contentPageRestorationIDs.count )
    {
        return nil;
    }

    return [self viewControllerAtIndex:index ];

}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
   NSLog(@"willTransitionToViewControllers %@ %lu",pendingViewControllers,(unsigned long)index);

}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
NSLog(@"didFinishAnimating %@",previousViewControllers);
}


Source: ios

Leave a Reply