ios - 以编程方式更改 UIPageViewController 的页面不会更新 UIPageControl

标签 ios iphone cocoa-touch

在我的自定义 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:

- (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];
    }
}

如果我不能用“属于”我的 UIPageViewControllerUIPageControl 来做到这一点,我将尝试自己制作。但如果这是可能的,那就太好了!

最佳答案

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

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController

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

所以您需要编写的代码块是:

- (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;
}

希望这能解决您的问题。 :)

关于ios - 以编程方式更改 UIPageViewController 的页面不会更新 UIPageControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554877/

相关文章:

ios - 如何让 titleView 在推送/弹出转换期间保留在 UINavigationBar 中,如 Facebook 应用程序?

cocoa-touch - os x 相当于ios的rootViewController

iphone - 设置 bundle 的自定义 View Controller ?

ios - 自定义 Segue 中的 UIKit 动态

iphone - 带有HTML标签的XML字符串

iphone - 等效于Objective-C的json_encode

iphone - Amazon S3 getDate() API 调用?

ios - 对于订阅系列中具有不同持续时间的自动续订订阅,Apple IAP 收据看起来如何?

ios - 从 cloudkit swift 获取不同的值

iphone - iPhone应用程序的文档目录是/var/mobile/Documents还是/var/mobile/Library/AppName?