Turn page programmatically in UIPageViewController when using UIPageViewControllerDataSource?
Solution 1
The Method mentioned in the answer ( setViewControllers:direction:animated:completion:
) sets the current view controller and is exactly what you need.
With the first param you define the view controller(s), you want to display. Use one if you don't have a spine, if you book is left/right use two.
Solution 2
Old question but the following implementation calls delegate and dataSource methods for turning to previous or next page (But doesn't check if delegate or dataSource is set):
- (IBAction)navigateReverse:(id)sender
{
UIViewController *controller = [self.dataSource pageViewController:self viewControllerBeforeViewController:self.designViewControllers[self.currentIndex]];
if (!controller)
{
return;
}
NSArray *newViewControllers = @[controller];
NSArray *previousViewControllers = self.viewControllers;
__weak __typeof(self) weakSelf = self;
[self.delegate pageViewController:self willTransitionToViewControllers:newViewControllers];
[self setViewControllers:newViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
[weakSelf.delegate pageViewController:weakSelf didFinishAnimating:finished previousViewControllers:previousViewControllers transitionCompleted:finished];
}];
}
- (IBAction)navigateForward:(id)sender
{
UIViewController *controller = [self.dataSource pageViewController:self viewControllerAfterViewController:self.designViewControllers[self.currentIndex]];
if (!controller)
{
return;
}
NSArray *newViewControllers = @[controller];
NSArray *previousViewControllers = self.viewControllers;
__weak __typeof(self) weakSelf = self;
[self.delegate pageViewController:self willTransitionToViewControllers:newViewControllers];
[self setViewControllers:newViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
[weakSelf.delegate pageViewController:weakSelf didFinishAnimating:finished previousViewControllers:previousViewControllers transitionCompleted:finished];
}];
}
Solution 3
I've created a easy project with that functionality:
https://github.com/delarcomarta/AMPageViewController
Hope this can help you.
Jens Kohl
I'm an iOS and Android developer in a corporate environment.
Updated on June 13, 2022Comments
-
Jens Kohl almost 2 years
I have an
UIPageViewController
and a class which conforms toUIPageViewControllerDataSource
and manages the UIViewControllers which UIPageViewController displays. It all runs well when turning the pages is initiated by the user. But now I want to do that programmatically.I've found that there is a very similar question here on SO which answers the question for a static amount of ViewControllers: Is it possible to Turn page programmatically in UIPageViewController?
But I'm unable to adapt the answers to the use of UIPageViewControllerDataSource. I was thinking that there must be something like:
- (void)setCurrentViewController:(UIViewController *)viewController
I suppose I'm just overlooking something here.
-
Rashmi Ranjan mallick almost 9 yearsCan you please elaborate the last line of your answer which says "Use one if you don't have a spine, if you book is left/right use two."
-
Christian Kreiter almost 7 years@RashmiRanjanmallick haha! He's referring to the ability for iPads to have two pages showing on the screen at once. If the pages on the screen are a "book" with a "spine," then you should set two view controllers as active at once. Otherwise, just show one.