I have been struggling with this issue for the last few days and after all this juggling I have figured out that all I need is the current Index from the datasource method to up
I struggled with this too and none of the other solutions worked for me, but was finally able to solve it using a delegate method:
First in PageContentViewController.h Add the following property:
@property NSUInteger pageIndex;
Second create a delegate protocol in the PageContentViewController (not the PageViewController).
In PageContentViewController.h:
@protocol PageContentDelegate
- (void)sendBackPageIndex:(NSUInteger)index;
@end
@interface LKFPageContentViewController : (UIViewController)
@property (weak, nonatomic) id delegate;
....
@end
Third in PageContentViewController.m perform delegate method in viewDidAppear
//Note that this must be done in viewDidAppear not viewDidLoad
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
[self.delegate sendBackPageIndex:self.pageIndex];
Fourth, in PageViewController.m implement the delegate method
#pragma mark - PageContentView delegate methods
- (void)sendBackPageIndex:(NSUInteger)index {
//This is where you get your current page index
NSUInteger currentPageIndex = index;
}
Fifth, in PageViewController.m make sure you pass the pageIndex value and set the delegate
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.arrayOfTitles count] == 0) || (index >= [self.arrayOfTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.pageIndex = index;
pageContentViewController.delegate = self;
return pageContentViewController
}