In my application,I have a UIPageViewController.Sometime the following two functions are not calling properly.
pageViewController:ViewControllerBeforeViewController
pageViewController:ViewControllerAfterViewController
I have a page indicator which is triggered from these two functions.IT is not working in sync with the current page sometimes. Here is the code that i am using in my application.
-(void)setPageView
{
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
NSArray *viewControllers ;
currentTab = 0;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
ContentCardListViewController *startingViewController = (ContentCardListViewController*)[self viewControllerAtIndex:0];
viewControllers = @[startingViewController];
}
else
{
IpadContentCardCollectionViewController *startingViewController = (IpadContentCardCollectionViewController*)[self viewControllerAtIndex:0];
viewControllers = @[startingViewController];
}
[self setTabTextColor:0];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
//self.pageViewController.view.frame = _pagerView.frame;
self.pageViewController.view.frame = CGRectMake(0, 0, _pagerView.frame.size.width, _pagerView.frame.size.height);
[self addChildViewController:_pageViewController];
[self.pagerView addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
#pragma mark - PageViewController
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
index = ((ContentCardListViewController*) viewController).pageIndex;
}
else
{
index=((IpadContentCardCollectionViewController*) viewController).pageIndex;
}
[self setTabTextColor:(int)index];
if (index == 0) {
return nil;
}
currentTab = (int)index;
// if (index<4) {
// [_horizontalTabScrollView setContentOffset:CGPointMake(self.horizontalTabScrollView.contentOffset.x-50,self.horizontalTabScrollView.contentOffset.y) animated:YES];
//
// }
index--;
NSLog(@"Befor nw=%d",(int)index);
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
index = ((ContentCardListViewController*) viewController).pageIndex;
}
else
{
index = ((IpadContentCardCollectionViewController*) viewController).pageIndex;
}
currentTab = (int)index;
[self setTabTextColor:(int)index];
if (index == NSNotFound) {
return nil;
}
index++;
if (index ==6) {
return nil;
}
// if (index>3) {
// [_horizontalTabScrollView setContentOffset:CGPointMake(self.horizontalTabScrollView.contentOffset.x+50,self.horizontalTabScrollView.contentOffset.y) animated:YES];
//
// }
NSLog(@"After nw=%d",(int)index);
return [self viewControllerAtIndex:index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
// if (index >6) {
// return nil;
// }
ContentCardListViewController *contentCardListViewController ;
IpadContentCardCollectionViewController *contentCardIpad ;
// Create a new view controller and pass suitable data.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
contentCardListViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentCardViewController"];
contentCardListViewController.pageIndex = index;
return contentCardListViewController;
}
contentCardIpad = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentCardViewControllerIpad"];
contentCardIpad.pageIndex = index;
return contentCardIpad;
}
Can anyone please check the index values?If I am giving it wrong or not.Thanks.