Turn page programmatically in UIPageViewController when using UIPageViewControllerDataSource?

天涯浪子 提交于 2019-12-19 10:07:47

问题


I have an UIPageViewController and a class which conforms to UIPageViewControllerDataSource 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.


回答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.




回答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];
    }];
}



回答3:


I've created a easy project with that functionality:

https://github.com/delarcomarta/AMPageViewController

Hope this can help you.



来源:https://stackoverflow.com/questions/11938149/turn-page-programmatically-in-uipageviewcontroller-when-using-uipageviewcontroll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!