Changing UIPageViewController's page programmatically doesn't update the UIPageControl

后端 未结 7 1746
悲哀的现实
悲哀的现实 2020-12-09 17:39

In my custom UIPageViewController class:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
           


        
7条回答
  •  长情又很酷
    2020-12-09 18:19

    to update your UIPageControl indicator, you need to implement one data source method of UIPageViewController (the UIPageViewControllerDataSource method) :

    -(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
    

    This method is responsible for updating the page control indicator (when you use UIPageViewController). You only need to return the currentpage value in this method. The Method gets called by default when you use/make a call for setViewControllers on your custom UIPageViewController.

    So the chunk of code that you need to write is:

    - (void)scrollToNext
    {
        UIViewController *current = self.viewControllers[0];
        NSInteger currentIndex = [self.model indexForViewController:current];
        UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
        if (nextController) {
            NSArray *viewControllers = @[nextController];
           // This changes the View Controller and calls the presentationIndexForPageViewController datasource method
           [self setViewControllers:viewControllers
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:YES
                      completion:nil];
    }
    
    - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
    {
        return currentIndex;
    }
    

    Hope this solves your problem. :)

提交回复
热议问题