0

I'm trying to add UIPageViewController to my RootViewController in code. I removed storyboard file according to Xcode 5 without Storyboard and ARC and adding “regular” ViewController works perfectly. I adoped methods according to UIPageViewControllerDataSource

Also I tried to simplify logic (instead of implementing ModelController from starting PageView project I add showVCWithIndex: method to pick 1 of 3 VC from vcArray and write it to Mutable Array with 1 VC. Hope you can tell me if I was right or wrong about that.

.m

#import "RootPageViewController.h"
#import "ViewController0.h"
#import "ViewController1.h"
#import "ViewController2.h"

@interface RootPageViewController ()

@property (strong, nonatomic) ViewController0 *vController0;
@property (strong, nonatomic) ViewController1 *vController1;
@property (strong, nonatomic) ViewController2 *vController2;

@property (strong, nonatomic) UIPageViewController *pageViewController;

@property (strong, nonatomic) NSArray *vcArray;
@property (strong, nonatomic) NSMutableArray *viewController;

@end

@implementation RootPageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageViewController.dataSource = self;

    _vController0 = [[ViewController0 alloc] init];
    _vController1 = [[ViewController1 alloc] init];
    _vController2 = [[ViewController2 alloc] init];


    NSUInteger index = 0;
    NSMutableArray *viewController = [NSMutableArray arrayWithObjects:[self showVCWithIndex:index], nil];

    [self.pageViewController setViewControllers:viewController
                                      direction:UIPageViewControllerNavigationDirectionForward
                                       animated:NO
                                     completion:nil];

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

    CGRect pageViewRect = self.view.bounds;
    self.pageViewController.view.frame = pageViewRect;

    [self.pageViewController didMoveToParentViewController:self];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.vcArray indexOfObject:viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self showVCWithIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.vcArray indexOfObject:viewController];

    if (index == 2 || index == NSNotFound) {
        return nil;
    }

    index++;
    return [self showVCWithIndex:index];
}

- (UIViewController *)showVCWithIndex: (NSUInteger)index
{
    self.vcArray = [NSArray arrayWithObjects: _vController0, _vController1, _vController2, nil];

    UIViewController *currentVC = [self.vcArray objectAtIndex:index];

    return currentVC;
}

@end

EDIT got the solution (this code works, but I'm not sure if I am 100% right)

Community
  • 1
  • 1
johann
  • 19
  • 5

2 Answers2

0

When you set the view controllers in your UIPageViewController it has to be an array with only one view controller. The method -setViewControllers:direction:animated:completion: is so misleading. I had the same problem as you when I created a page view controller for the first time.

To allow your page view controller to display other view controllers you return them in the UIPageViewControllerDataSource methods -pageViewController:viewControllerBeforeViewController: and -pageViewController:viewControllerAfterViewController:.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
0

When you call...

[self.pageViewController setViewControllers:[vcArray objectAtIndex:0] 
                                  direction:UIPageViewControllerNavigationDirectionForward 
                                   animated:NO
                                 completion:nil];

... you are setting a nil view controller to the pagination view controller.

[vcArray objectAtIndex:0] points to _vController0 which is, in that moment, nil (you haven't instantiated it yet).

Before set you view controllers into the pagination, you must get it from the storyboard using [self.storyboard instatiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"] or simply calling a alloc init, if you are using Xibs.

Gustavo Barbosa
  • 1,340
  • 1
  • 17
  • 27