的UIPageViewController

的UIPageViewController

我需要一个动态的UIPageViewController,页面数取决于我数组中项目的数量。

我对此有两种看法。一个用于第一页,另一个用于其他页。它将始终超过2页(我的阵列上有两个项目)。

我正在尝试使用固定值进行测试。但是分页是不正确的。索引不正确。

第一个问题,presentationCountForPageViewController [_vc count]返回nil;

第二个问题,分页不正确。我到达最后一页,第一个视图消失了,因此,如果我返回,则总是转到第二个视图...

请帮忙!

    - (void)viewDidLoad {
        [super viewDidLoad];

        FirstViewController *agenda = (FirstViewController*)[self viewControllerAtIndex:0];
        [_vc addObject:@"FirstViewController"];
        [_vc addObject:@"SecondViewController"];


        self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
        self.pageViewController.dataSource = self;


        NSArray *viewControllers = [NSArray arrayWithObject:agenda];

        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];


        [self addChildViewController:_pageViewController];
        [self.view addSubview:_pageViewController.view];
        [self.pageViewController didMoveToParentViewController:self];

        UIPageControl *pageControlAppearance = [UIPageControl appearanceWhenContainedIn:[UIPageViewController class], nil];
        pageControlAppearance.pageIndicatorTintColor = [UIColor lightGrayColor];
        pageControlAppearance.currentPageIndicatorTintColor = [UIColor darkGrayColor];


    }




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

    NSUInteger index = [_vc indexOfObject:viewController];

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

    index--;

    //notice here I call my instantiation method again essentially duplicating work I have already done!
    return [self viewControllerAtIndex:index];
}

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

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

    index++;
    return [self viewControllerAtIndex:index];
}


#pragma mark - Page View Controller Data Source

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{

    if (index == 0){
        FirstViewController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

        return firstController;
    }
    else{
        SecondViewController *secondController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
        return secondController;
    }

}

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

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

最佳答案

根据评论/聊天,需要解决一些问题:

  • 您需要实例化_vc数组
  • 您需要将UIViewController对象添加到此数组,而不是当前使用的字符串
  • 您需要修改viewControllerBeforeViewControllerviewControllerAfterViewController方法,以从_vc数组返回相关元素,并记住要测试索引是否保持在数组范围之内,以防止滑过最后一页或在第一页(议程)之前。
  • 关于ios - 具有多个 View 的UIPageViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29055404/

    10-10 14:50