Does anyone know what am I missing in order to add manually my own view controllers to the UIPageViewController methods?
I currently have this and I do not know how
I see a few problems here. I'm a bit of a noob myself, so I don't know if this will fix everything in your code.
You need a UIPageViewController and then views for the individual pages that are instantiated when required. Passing an array of views isn't efficient (uses too much memory).
You need to implement the UIPageViewControllerDataSource protocol in your a class that sub-classes from UIPageViewController. Most (but not all) of the code in your snippet will go here. See the class reference or this tutorial for an explanation.
You're probably doing more work than you need to with the gesture recognizers. If you use IBOutlet, you can do the plumbing through the interface builder.
Hope this helps.
Try this function:
setViewControllers:direction:animated:completion:
Sets the view controllers to be displayed.
- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion
Parameters
viewControllers: The view controller or view controllers to be displayed.
direction: The navigation direction.
animated: A Boolean value that indicates whether the transition is to be animated.
completion: A block to be called when the page-turn animation completes.
The block takes the following parameters:
finished YES if the animation finished; NO if it was skipped.
The way you've set this up isn't quite how UIPageViewController
works.
The first thing to understand is that setViewControllers:direction:animated:completion
only sets the visible view controllers, not all the view controllers you may wish to display to the user. (To wit, if I try to set an array of three controllers as in your code, I get an exception.)
Secondly, if you have more than one visible page (or two, depending on spine location), you must implement the UIPageViewControllerDataSource
protocol to provide the view controllers the page view controller will display.
Here's a short example (implemented as a subclass of UIPageViewController
, but would work with a child page view controller, too... just replace self.dataSource = ...
with myPageViewController.dataSource = ...
)
@implementation MyPageViewController {
// we keep our page view controllers in an array, but we could just as easily
// instantiate them on the fly in the data source methods
NSArray *_pages;
}
- (void)setupPages {
/*
* set up three pages, each with a different background color
*/
UIViewController *a = [[UIViewController alloc] initWithNibName:nil bundle:nil];
a.view.backgroundColor = [UIColor redColor];
UIViewController *b = [[UIViewController alloc] initWithNibName:nil bundle:nil];
b.view.backgroundColor = [UIColor greenColor];
UIViewController *c = [[UIViewController alloc] initWithNibName:nil bundle:nil];
c.view.backgroundColor = [UIColor blueColor];
_pages = @[a, b, c];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupPages];
self.dataSource = self;
// set the initially visible page's view controller... if you don't do this
// you won't see anything.
[self setViewControllers:@[_pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
}];
}
#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (nil == viewController) {
return _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx >= [_pages count] - 1) {
// we're at the end of the _pages array
return nil;
}
// return the next page's view controller
return _pages[idx + 1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (nil == viewController) {
return _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx <= 0) {
// we're at the end of the _pages array
return nil;
}
// return the previous page's view controller
return _pages[idx - 1];
}