Simple UIPageViewController

Undefined Programming

Simple UIPageViewController

To use UIPageViewController effectively you would need an UIPageViewControllerDataSource, this will help to display the before and after UIViewController. Here is an simple implementation to get you started:

#import <UIKit/UIKit.h>

@interface PYPageViewController : UIPageViewController
@property NSMutableArray *pages;
@end
#import "PYPageViewController.h"

@interface PYPageViewController()
@end

@implementation PYPageViewController
@synthesize pages;

- (void)viewWillAppear:(BOOL)animated {
    [self setDataSource:(id)self]; //Use this class as the data source
    self.pages  = [[NSMutableArray alloc]initWithCapacity:3];
    [self.pages addObject:[self.storyboard instantiateViewControllerWithIdentifier:@"PYRootViewController"]];
    [self.pages addObject:[self.storyboard instantiateViewControllerWithIdentifier:@"PYNextViewController"]];
    [self.pages addObject:[self.storyboard instantiateViewControllerWithIdentifier:@"PYEndViewController"]];
    [self setViewControllers:[NSArray arrayWithObject:pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:true completion:nil];
    [super viewWillAppear:animated];
}

/* UIPageViewControllerDataSource */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    UIViewController *view = nil;

    if ([self.pages objectAtIndex:0] != viewController){
        for (NSUInteger i = [self.pages count]-1;i > 0 ; i--) {
            if ([self.pages objectAtIndex:i] == viewController){
                view = [self.pages objectAtIndex:i-1];
                break;
            }
        }
    }
    return view;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    UIViewController * view = nil;
    if ([self.pages objectAtIndex:[self.pages count]-1] != viewController){
        for (NSUInteger i = 0;i < [self.pages count]-1; i++) {
            if ([self.pages objectAtIndex:i] == viewController){
                view = [self.pages objectAtIndex:i+1];
                break;
            }
        }
    }
    return view;
}

@end