执行setViewControllers

时间:2015-06-26 15:52:48

标签: ios objective-c uipageviewcontroller

我看过this问题,但到目前为止它没有帮助我。当我以编程方式将UIPageViewControllersetViewControllers联系起来时,我的UIPageControl不会更新。

我有7个视图,页面控件在我的自定义UIPageViewController课程中设置:

    self.pageControl = [UIPageControl appearance];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor globalColor];
    self.pageControl.backgroundColor = [UIColor clearColor];
    self.pageControl.numberOfPages = TotalViewControllerCount; //static int TotalViewControllerCount = 7;

这是我的ViewController:

- (void) viewDidLoad {

    OBVideoViewController *obv = [self.storyboard instantiateViewControllerWithIdentifier:@"screen1"];
    OBVideoViewController *obv1 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen2"];
    OBVideoViewController *obv2 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen3"];
    OBVideoViewController *obv3 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen4"];
    OBVideoViewController *obv4 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen5"];
    OBVideoViewController *obv5 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen6"];
    OBVideoViewController *obv6 = [self.storyboard instantiateViewControllerWithIdentifier:@"screen7"];

    self.viewControllerIDs = [[NSArray alloc]initWithObjects:obv, obv1, obv2, obv3, obv4, obv5, obv6, nil];
    obv.pageIndex = 0;
    self.currentIndex = 0;
    // Create page view controller
    self.pageViewController = [[OnboardingPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; //[self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;
    [self.pageViewController setViewControllers:@[obv] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [self.viewControllerIDs indexOfObject:viewController];
    if(currentIndex == 0)
        return nil;
    OBVideoViewController *obv = [self.viewControllerIDs objectAtIndex:currentIndex - 1];
    obv.pageIndex = currentIndex - 1;
    self.currentIndex = currentIndex--;
    obv.delegate = self;
    return obv;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [self.viewControllerIDs indexOfObject:viewController];
    if(currentIndex == self.viewControllerIDs.count - 1)
        return nil;
    OBVideoViewController *obv = [self.viewControllerIDs objectAtIndex:currentIndex + 1];
    DLog(@"%@",obv.restorationIdentifier);
    obv.pageIndex = currentIndex + 1;
    self.currentIndex = currentIndex++;
    obv.delegate = self;
    return obv;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    NSInteger index = [self.viewControllerIDs indexOfObject:[pageViewController.viewControllers firstObject]];
    DLog(@"%lu",index);
    DLog(@"%@", [[pageViewController.viewControllers firstObject] restorationIdentifier]);
    return index;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return [self.viewControllerIDs count];
}

- (void)scrollRight
{
    NSUInteger currentIndex = [self.viewControllerIDs count] - 1;
    OBVideoViewController *obv = [self.viewControllerIDs objectAtIndex:[self.viewControllerIDs count] -1 ];
    obv.pageIndex = currentIndex + 1;
    self.currentIndex = currentIndex + 1;
    obv.delegate = self;
    self.currentIndex = currentIndex;
    [self.pageViewController setViewControllers:@[obv] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}

同样,如果我只是手动翻页,pageControl更新就好了,但如果我调用setViewControllers跳转到最后一页,我的pageControl就不会更新。

0 个答案:

没有答案