UIPageViewController doesn't change page

I have a view controller implementing UIPageViewControllerDataSource delegate, and it contains a UIPageViewController.

My issue is that, after 3 hours of tutorials and reading, I still don't understand why the UIPageController reacts to swipe by moving it's content, but it doesn't change the page if the scroll is enough. It's always stuck on the first page.

So this is my .h file

#import <UIKit/UIKit.h>
@interface BreathePageViewController : UIViewController <UIPageViewControllerDataSource> 
@property (strong, nonatomic) UIPageViewController *pageController;
@end

And this is my .m file

#import "BreathePageViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h.h"



@interface BreathePageViewController () {
    NSArray *pageViewControllerScreens;
    FirstViewController *firstViewController;
    SecondViewController *secondViewController;
    int pageIndex;
}

@end

@implementation BreathePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    pageIndex = 0;
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    [self.pageController setViewControllers:@[firstViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self.view addSubview:self.pageController.view];
    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (pageIndex == 0) {
        return nil;
    }
    pageIndex--;
    return [self viewControllerAtIndex:pageIndex];   
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (pageIndex == 2) {
        return nil;
    }
    pageIndex++;        
    return [self viewControllerAtIndex:pageIndex];
}

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {

    UIViewController *vc;

    if (pageIndex == 0 ) {
        vc = [[FirstViewController alloc] init];
    }
    else if (pageIndex == 1) {
        vc = [[SecondViewController alloc] init];
    }

    return vc;

}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return 2;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}


@end

Now the strange stuff is that the pageViewController:viewControllerAfterViewController is never called. Can someone help me out? Thank you

Answers


After a night of sleep, I am going to to reply my own question.

I don't know why (any comment appreciated), but the problem was not on that view controller, but it was in the one I was creating it in.

In the parent view controller I was building the UIPageViewController like that:

BreathePageViewController  *pageController = [[BreathePageViewController alloc] init];
[pageController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:pageController.view];

And now I tried to move the declaration of pageViewController as an iVar. And it works. Hope to help someone else


Need Your Help

Could not load ruby textmate?

ruby-on-rails ruby terminal rvm textmate

On OS X, after installing Ruby 1.9.3 and Rails 4.0.0 via RVM, I get this annoying message every time I open a new terminal window:

Best practice for sharing data between a WCF service and a web site

c# wcf caching

I have a .net web page which displays a list of objects. I also have a WCF service which allows clients to submit new objects. This is all glued together via SQL Server.