Turn page programmatically in UIPageViewController when using UIPageViewControllerDataSource?

后端 未结 3 1448
灰色年华
灰色年华 2021-01-15 02:32

I have an UIPageViewController and a class which conforms to UIPageViewControllerDataSource and manages the UIViewControllers which UIPageViewContr

相关标签:
3条回答
  • 2021-01-15 03:18

    I've created a easy project with that functionality:

    https://github.com/delarcomarta/AMPageViewController

    Hope this can help you.

    0 讨论(0)
  • 2021-01-15 03:23

    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.

    0 讨论(0)
  • 2021-01-15 03:38

    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];
        }];
    }
    
    0 讨论(0)
提交回复
热议问题