以编程方式更改UIPageViewController的页面不会更新UIPageControl [英] Changing UIPageViewController's page programmatically doesn't update the UIPageControl

查看:176
本文介绍了以编程方式更改UIPageViewController的页面不会更新UIPageControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的自定义 UIPageViewController 类中:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.model = [[BSTCMWelcomingPageViewModel alloc] init];
        self.dataSource = self.model;
        self.delegate = self;

        self.pageControl = [UIPageControl appearance];
        self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    }
    return self;
}

然后我以编程方式设置当前 ViewController 当命中按钮时:

Then I programmatically set the current ViewController when a button is hit:

- (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, but PageControl doesn't update
        [self setViewControllers:viewControllers
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:YES
                      completion:nil];
        //Nothing happens!
        [self.pageControl setCurrentPage:currentIndex];

        //Error: _installAppearanceSwizzlesForSetter: Not a setter!
        [self.pageControl updateCurrentPageDisplay];
    }
}



如果我不能用 UIPageControl 属于我的 UIPageViewController 我会试图做自己的。但是如果这是可能的话,这将是很好的。

If I can't do this with the UIPageControl that "belongs" to my UIPageViewController I will just try to make my own. But it would be nice if this was possible tho!

推荐答案

更新您的UIPageControl指示器,需要实现一个数据源方法的UIPageViewController(UIPageViewControllerDataSource方法):

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

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController

此方法负责更新页面控制指示器(当您使用UIPageViewController时)。您只需要返回此方法中的当前页面值。当您在自定义UIPageViewController上使用/调用setViewController时,默认情况下会调用该方法。

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. :)

这篇关于以编程方式更改UIPageViewController的页面不会更新UIPageControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆