UIPageViewController自动滚动中的索引不起作用

时间:2015-03-04 21:13:23

标签: objective-c indexing uipageviewcontroller

我有一个UIPageViewController,可以通过设置这样的计时器自动滚动:

bannerTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(switchToNextBanner) userInfo:nil repeats:YES];

然而,每当我刷横幅时,我的索引就会搞砸了。它不会加载正在显示的索引的下一个索引。我该如何解决这个问题?

以下是我的UIPageViewController方法:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = ((BannerContentViewController *) viewController).bannerIndex;

    if (index == NSNotFound) {
        return nil;
    }

    if (index == 0) {
        index = [_bannersArray count] - 1;
    } else {
        index--;
    }

    return [self loadBannerViewAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = ((BannerContentViewController *) viewController).bannerIndex;

    if (index == NSNotFound) {
        return nil;
    }

    if (index == [_bannersArray count]) {
        index = 0;
    } else {
        index++;
    }

    return [self loadBannerViewAtIndex:index];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}

这是我在给定索引处加载横幅的方法:

- (BannerContentViewController *)loadBannerViewAtIndex:(NSUInteger)index {
    NSLog(@"Index: %lu", (unsigned long)index);

    BannerContentViewController *bannerContentVC = [self.storyboard instantiateViewControllerWithIdentifier:@"BannerContentView"];
    NSUInteger bannerCount = [_bannersArray count];

    if (index >= bannerCount) {
        bannerDictionary = self.allBannersAndPromosArray[0];
        bannerContentVC.bannerIndex = 0;

        nextBannerIndex = 1;
    } else {
        bannerDictionary = self.allBannersAndPromosArray[index];
        bannerContentVC.bannerIndex = index;

        nextBannerIndex = index + 1;
    }

    return bannerContentVC;
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码加载所需的viewController。我分享了您使用的代码,您可以根据自己的要求进行编辑。

您可以在setSelectedPageIndex上使用计时器并传递下一个索引。

-(void)setSelectedPageIndex:(NSInteger)selectedPageIndex
{
     _selectedPageIndex = selectedPageIndex;
     //setting current selected page index to visible on pageViewController
     [self.detailPageControl setViewControllers:@[[self.detailViewControllers objectAtIndex:selectedPageIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished) {

     }];
}


#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    NSUInteger index = [self.detailViewControllers indexOfObject:viewController];

    if ((index == NSNotFound) || (index == 0)) {
        return nil;
    }

    return self.detailViewControllers[--index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
     NSUInteger index = [self.detailViewControllers indexOfObject:viewController];

    if ((index == NSNotFound)||(index+1 >= [self.detailViewControllers count])) {
        return nil;
}

    return self.detailViewControllers[++index];
}

- (void)pageViewController:(UIPageViewController *)viewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (!completed){
        return;
    }
    else
    {
        UIViewController *currentView = [viewController.viewControllers objectAtIndex:0];
        NSInteger currentIndex = [self.detailViewControllers indexOfObject:currentView];
        //currentIndex is index of currently visible viewController
    }
}