Turn page programmatically in UIPageViewController when using UIPageViewControllerDataSource?

后端 未结 3 1451
灰色年华
灰色年华 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: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];
        }];
    }
    

提交回复
热议问题