UIPageViewController loads wrong page - ios

I am doing a tutorial in my iOS app. But I have a bug in my PageViewController.
My tutorial has 5 pages. But the second movement that I do to change page always appears the previous page repeatedly.
For example, appears page0, move to page1, and then when i want to appear page2, appears page1 again.
This occurs to both sides.
This is my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.pageTexts = #[#"",intro1, intro2, intro3, intro4];
self.index = 0;
self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init];
self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init];
self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init];
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
UIViewController* first = [self viewControllerUsed];
NSArray *viewControllers = [NSArray arrayWithObject:first];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (UIViewController *)viewControllerUsed{
if (self.index == 0) {
self.viewControllerPageContentOne=[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentOne"];
return self.viewControllerPageContentOne;
}
else if (self.index == 1)
{
self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"];
self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
return self.viewControllerPageContentTwo;
}
else if (self.index == 2)
{
self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"];
self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
return self.viewControllerPageContentTwo;
}
else if (self.index == 3)
{
self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"];
self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
return self.viewControllerPageContentTwo;
}
else if (self.index == 4)
{
self.viewControllerPageContentThree=[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentThree"];
self.viewControllerPageContentThree.titleText = self.pageTexts[self.index];
return self.viewControllerPageContentThree;
}
else{
return nil;
}
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
self.index--;
if (self.index < 0) {
self.index = [self.pageTexts count]-1;
}
return [self viewControllerUsed];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
self.index++;
if (self.index > [self.pageTexts count]-1) {
self.index = 0;
}
return [self viewControllerUsed];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageTexts count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
I suppose that there is an error in index pages but I don’t know it.
Thanks

I think your problem is that you are creating class level properties for view controllers. And that too you are initialising them in viewDidLoad
self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init];
self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init];
self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init];
My advise is to create view controllers and save them in an array like this:
UIViewController *page0 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentOne"];
UIViewController *page1 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[1];
UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[2];
UIViewController *page3 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[3];
UIViewController *page4 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerPageContentThree"];
[self.viewControllers addObject:page0];
[self.viewControllers addObject:page1];
[self.viewControllers addObject:page2];
[self.viewControllers addObject:page3];
[self.viewControllers addObject:page4];
And then in your viewControllerUsed simply return viewControllers from self.viewControllers like self.viewControllers[self.index].

Related

UIPageViewController viewControllerAfterViewController never called

I have a UIPageViewController that I've set up with an array as the datasource. I see the first page fine and the presentation count dots show up correctly, but when I try to drag the to the next page nothing happens. So I set up some breakpoints in the viewControllerAfterViewController as well as Before methods and they never get called on. The other dataSource methods for presentationCountForPageViewController get called on fine.
Any idea what I am missing? I've been going line by line with examples I've found on the web but can't find anything.
- (void)viewDidLoad
{
[super viewDidLoad];
UIPageViewController *pageController = self.pageController;
pageController.dataSource = self;
UIChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[initialViewController];
[pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:pageController];
[self.view addSubview:pageController.view];
[pageController didMoveToParentViewController:self];
}
- (UIPageViewController *)pageController
{
return !_pageController ? _pageController =
({
UIPageViewController *value = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
value.view.frame = self.view.bounds;
value;
}) : _pageController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pa geViewController viewControllerBeforeViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
return (index == 0 ) ? nil : [self viewControllerAtIndex:--index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
index++;
return (index == self.myDataSourceArray.count) ? nil : [self viewControllerAtIndex:index];
}
- (UIChildViewController *)viewControllerAtIndex:(NSUInteger)index
{
UIChildViewController *childViewController = [UIChildViewController new];
childViewController.view.frame = self.pageController.view.bounds;
childViewController.index = index;
...
return childViewController;
}
- (NSInteger)presentationCountForPageViewController: (UIPageViewController *)pageViewController
{
// The number of items reflected in the page indicator.
return self.myDataSourceArray.count;
}
- (NSInteger)presentationIndexForPageViewController:(UI PageViewController *)pageViewController
{
// The selected item reflected in the page indicator.
return 0;
}
The problem was that in including my UIViewController subclass which encapsulated the UIPageViewController, I forgot in the parent view controller's viewDidLoad to include addChildViewController and didMoveToParentViewController
[self addChildViewController:myViewController];
[self.view addSubview:myViewController.view];
[myViewControllerController didMoveToParentViewController:self];
Fixed the problem.

UiScrollView not working the way i want to

I have been trying for a while to get UIScrollView to get working. Ive come so far that it does indeed scroll but not to the ViewController i want to. And i have no idea how to do it so some help from here would be nice.
This is how the code looks like for ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:[NSNull null]];
[controllers addObject:[NSNull null]];
self.viewControllers = controllers;
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize =
CGSizeMake(CGRectGetWidth(self.scrollView.frame) * 2, CGRectGetHeight(self.scrollView.frame));
self.scrollView.showsHorizontalScrollIndicator = YES;
self.scrollView.delegate = self;
self.pageControl.numberOfPages = 2;
self.pageControl.currentPage = 0;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadScrollViewWithPage:(NSUInteger)page
{
if (page >= 2)
return;
// replace the placeholder if necessary
ViewController2 *controller = [self.viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[ViewController2 alloc] init];
[self.viewControllers replaceObjectAtIndex:page withObject:controller];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self addChildViewController:controller];
[self.scrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
}
// at the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
NSUInteger page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// a possible optimization would be to unload the views+controllers which are no longer visible
}
- (void)gotoPage:(BOOL)animated
{
NSInteger page = self.pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect bounds = self.scrollView.bounds;
bounds.origin.x = CGRectGetWidth(bounds) * page;
bounds.origin.y = 0;
[self.scrollView scrollRectToVisible:bounds animated:animated];
}
- (IBAction)changePage:(id)sender
{
[self gotoPage:YES]; // YES = animate
}
So what happens is the first view gets loaded. And i can scroll but it scrolls to an empty view. What i would like to do is that it scrolls to ViewController2
How can i implement for it to do so? (horizontally)
EDIT1, trying to implement what matt suggested
- (void)viewDidLoad {
[super viewDidLoad];
self.vcIdentifiers = #[#"PageContentViewController", #"PageContentViewController1"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
UIViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.vcIdentifiers count] == 0) || (index >= self.vcIdentifiers.count)) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
//return [self.pageTitles count];
return 2;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
I am thinking of using the storyboard id's that my viewcontrollers have, svaed in VCidentifiers.
What i dont get is how to instantiate the viewcontrollers in the method viewControllerAtIndex. The way it is written now, it will only create PageContentViewController, when it should for index 0 create PageContentViewController and for index 1 PageContent2Controller.
What am i missing?
A scroll view is for scrolling between existing views, not for dynamically loading views from view controllers. You can use view controllers in this way, and we used to have to do it sometimes, but (as you've discovered) it is difficult.
I recommend that you drop your scroll view and use UIPageViewController in a scroll style instead. It solves all the problem for you when what you want you to do is scroll between view controllers' views.

Loading multiple View controller in UIPageViewController - Swiping shows blank screen

I am loading multiple UIViewControllers in UIPageViewController with each UIViewController having their own navigation bar. I have implemented all the required data source and delegate methods for UIPageViewController and it showing up correctly.
But when I scroll my view controller horizontally, more often, I see a white blank screen instead of my view controller. Below is my code.
Any clue what could be wrong here.
PS: I tried by removing the IF condition in my viewControllerForRequestID method and returning a new controller every time. This works but then I end up with one view controller showing up twice in pagination.
- (UIViewController *)pageViewController:(UIPageViewController *)iPageViewController viewControllerBeforeViewController:(UIViewController *)iViewController {
[self.pageControl setCurrentPage:self.selectedRequestIndex];
if (self.selectedRequestIndex == 0) {
return nil;
}
NSUInteger aNewSelectedRequestIndex = self.selectedRequestIndex - 1;
NSString *aPreviousRequestID = [self.paginationRequestIDList[aNewSelectedRequestIndex] stringValue];
self.selectedRequestIndex = aNewSelectedRequestIndex;
self.selectedRequestID = aPreviousRequestID;
return [self viewControllerForRequestID:self.selectedRequestID];
}
- (UIViewController *)pageViewController:(UIPageViewController *)iPageViewController viewControllerAfterViewController:(UIViewController *)iViewController {
[self.pageControl setCurrentPage:self.selectedRequestIndex];
if (self.selectedRequestIndex == NSNotFound || self.selectedRequestIndex >= self.paginationRequestIDList.count - 1) {
return nil;
}
NSUInteger aNewSelectedRequestIndex = self.selectedRequestIndex + 1;
NSString *aNextRequestID = self.paginationRequestIDList[aNewSelectedRequestIndex];
self.selectedRequestIndex = aNewSelectedRequestIndex;
self.selectedRequestID = aNextRequestID;
return [self viewControllerForRequestID:self.selectedRequestID];
}
- (MyNavigationViewController *)viewControllerForRequestID:(id)iRequestID {
NSString *aRequestID = iRequestID;
// Safe check to avoid numbers in request ID
if (![iRequestID isKindOfClass:[NSString class]]) {
aRequestID = [iRequestID stringValue];
}
#synchronized (self) {
if ([self.viewControllers containsObjectForKey:iRequestID]) {
MyNavigationViewController *aNavigationController = self.viewControllers[iRequestID];
return aNavigationController;
}
MyDataModel *aRequestData = self.savedRequestData[iRequestID];
MyDataModelController *aRequestInfoController = [[MyDataModelController alloc] initWithRequestData:aRequestData];
aRequestInfoController.delegate = self;
MyNavigationViewController *aNavigationController = [[MyNavigationViewController alloc] initWithRootViewController:aRequestInfoController];
[self.viewControllers setObject:aNavigationController forKey:iRequestID];
return aNavigationController;
}
}

'[<InstructionContentViewController] this class is not key value coding-compliant for the key instructionImageView

I have a slight problem with my code. I keep getting the following message appear when I try to I have two page view controllers in my application. Both are being used for different things. I copied and pasted my coding for the first page controller I implemented and then I changed around the names to prevent conflicts. I recently started to recieve this error message and now my program can no longer work. Can anyone please tell me why this happens?
I also get an error which says method 'pageViewController:viewControllerBeforeViewController:' in protocol not implemented. Im not too sure if this is a linked problem, but does anyone know why I get this?
#import "StorageViewController.h"
#import "InstructionContentViewController.h"
#interface StorageViewController ()
#end
#implementation StorageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the data model
_pageInstructTitles = #[#"Over 200 Tips and Tricks", #"Discover Hidden Features", #"Bookmark Favorite Tip", #"Free Regular Update"];
_pageInstructImages = #[#"instructions1.png", #"instructions2.png", #"instructions3.png", #"instructions4.png"];
// Create page view controller
self.instructViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"InstructionViewController"];
self.instructViewController.dataSource = self;
InstructionContentViewController *startingInstructViewController = [self viewControllerAtIndex:0];
NSArray *viewInstructControllers = #[startingInstructViewController];
[self.instructViewController setViewControllers:viewInstructControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.instructViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_instructViewController];
[self.view addSubview:_instructViewController.view];
[self.instructViewController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startInstructWalkthrough:(id)sender {
InstructionContentViewController *startingInstructViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingInstructViewController];
[self.instructViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
#pragma mark - Page View Controller Data Source Methods:
- (UIViewController *)instructViewController:(UIPageViewController *)instructViewController viewControllerBeforeViewController:(UIViewController *)viewInstructController
{
NSUInteger index = ((InstructionContentViewController*) viewInstructController).instructPageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)instructViewController:(UIPageViewController *)instructViewController viewControllerAfterViewController:(UIViewController *)viewInstructController
{
NSUInteger index = ((InstructionContentViewController*) viewInstructController).instructPageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageInstructTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (InstructionContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageInstructTitles count] == 0) || (index >= [self.pageInstructTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
InstructionContentViewController *instructionContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"InstructionContentViewController"];
instructionContentViewController.instructImageFile = self.pageInstructImages[index];
instructionContentViewController.instructTitleText = self.pageInstructTitles[index];
instructionContentViewController.instructPageIndex = index;
return instructionContentViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)instructViewController
{
return [self.pageInstructTitles count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)instructViewController
{
return 0;
}
#end

UIPageViewController blanking page

I've been trying to use the UIPageViewController to display 3 different nibs for a few days on and off now and have almost got it working. I still have one weird bug that I cant figure out.
Basically the app starts, I can scroll between the 3 pages one after another with out any problems, eg:
Page1->Page2->Page3
and then back to the start:
Page3->Page2->Page1.
No Problems. The issue is that if I scroll, for example from Page3->Page2, then BACK to Page3, Page3 Dissappears when it snaps into place. If I scroll to where a forth page would be, then I get Page3. Here is the code relevant to the UIPageViewController, the nibs and the delegate methods for the UIPageViewController:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
indexTest = 0;
Page1 *p1 = [[Page1 alloc]initWithNibName:#"Page1" bundle:nil];
p1.view.tag = 1;
Page2 *p2 = [[Page2 alloc]initWithNibName:#"Page2" bundle:nil];
p2.view.tag = 2;
Page3 *p3 = [[Page3 alloc]initWithNibName:#"Page3" bundle:nil];
p3.view.tag = 3;
NSArray *arr = [[NSArray alloc] initWithObjects:p1,nil];
viewControllers = [[NSMutableArray alloc] initWithObjects:p1,p2,p3, nil];
[self.pageViewController setViewControllers:arr direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.dataSource = self;
[self addChildViewController:self.pageViewController];
[[self view] addSubview:[self.pageViewController view]];
[self.pageViewController didMoveToParentViewController:self];
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
#pragma mark - page view controller stuff
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (indexTest > 0) {
switch (indexTest) {
case 1:{
NSLog(#"NO page is BEFORE current page");
break;
}
case 2:{
NSLog(#"Page BEFORE is Page: %#", [NSString stringWithFormat:#"%#",[viewControllers objectAtIndex:0] ] );
indexTest--;
return [viewControllers objectAtIndex:0];
break;
}
default:{
NSLog(#"PROBLEM in viewBEFORE, indexTest = %d!!!!", indexTest);
break;
}
}
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (indexTest < NUM_OF_PAGES) {
switch (indexTest) {
case 0:{
NSLog(#"Page AFTER is Page: %#", [NSString stringWithFormat:#"%#",[viewControllers objectAtIndex:1] ] );
indexTest++;
return [viewControllers objectAtIndex:1];
break;
}
case 1:{
NSLog(#"Page AFTER is Page: %#", [NSString stringWithFormat:#"%#",[viewControllers objectAtIndex:2] ] );
indexTest++;
return [viewControllers objectAtIndex:2];
break;
}
case 2:{
NSLog(#"No pages AFTER this current page %d", indexTest);
break;
}
default:{
NSLog(#"PROBLEM in viewAFTER, indexTest = %d!!!!", indexTest);
break;
}
}
}
return nil;
}
Finally the page index dots code
#pragma mark - dot controller
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
// The number of items reflected in the page indicator.
return NUM_OF_PAGES;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return 0;
}
Any and all help is much appreciated, I think I'm just doing something silly that I cant see as I'm so close to it fully working. If anythings not clear or I haven't give enough information please let me know and I'll answer it as best as I can.
Thanks
I think your indexTest logic is wrong, it's showing the wrong indexes for the viewControllers. I wouldn't use those methods to keep track of the index as they can be called even when the views aren't actually going to change.
Instead you could replace the selection of view controllers like this:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
int vcIndex = [self.viewControllers indexOfObject:viewController];
if (vcIndex > 0) {
return [self.viewControllers objectAtIndex:vcIndex-1];
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
int vcIndex = [self.viewControllers indexOfObject:viewController];
if (vcIndex < NUM_OF_PAGES-1) {
return [self.viewControllers objectAtIndex:vcIndex+1];
}
return nil;
}
And if you need to keep track of the current index, use the delegate method provided.
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if (completed && finished) {
UIViewController *currentVC = pageViewController.viewControllers[0];
indexTest = [self.viewControllers indexOfObject:currentVC];
}
}
Just note that if your using this method, don't call setViewControllers: amimated:YES as this will lose track of the correct position.
Side note: I too have been struggling with UIPageViewController and found it was quicker and easier to write my own version. I think it has too many bugs.

Resources