2

几天来,我一直在尝试使用 UIPageViewController 来显示 3 个不同的笔尖,并且几乎可以正常工作。我仍然有一个我无法弄清楚的奇怪错误。

基本上应用程序启动,我可以一个接一个地在 3 个页面之间滚动,没有任何问题,例如:

第1页->第2页->第3页

然后回到开始:

第3页->第2页->第1页。

没问题。问题是,如果我滚动,例如从Page3->Page2滚动,然后返回到Page3, Page3 在卡入到位时消失。如果我滚动到第四页所在的位置,那么我会得到 Page3。这是与 UIPageViewController、nib 和 UIPageViewController 的委托方法相关的代码:

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageViewController.delegate = self;
    [[self.pageViewController view] setFrame:[[self view] bounds]];

    indexTest = 0;

    Page1 *p1 = [[Page1 alloc]initWithNibName:@"Page1" bundle:nil];
    p1.view.tag = 1;

    Page2 *p2 = [[Page2 alloc]initWithNibName:@"Page2" bundle:nil];
    p2.view.tag = 2;

    Page3 *p3 = [[Page3 alloc]initWithNibName:@"Page3" bundle:nil];
    p3.view.tag = 3;

    NSArray *arr = [[NSArray alloc] initWithObjects:p1,nil];
    viewControllers = [[NSMutableArray alloc] initWithObjects:p1,p2,p3, nil];

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

    self.pageViewController.dataSource = self;

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

    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}


#pragma mark - page view controller stuff
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    if (indexTest > 0) {
        switch (indexTest) {

            case 1:{
                NSLog(@"NO page is BEFORE current page");
                break;
            }
            case 2:{
                NSLog(@"Page BEFORE is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:0] ] );
                indexTest--;
                return [viewControllers objectAtIndex:0];
                break;
            }
            default:{
                NSLog(@"PROBLEM  in viewBEFORE, indexTest = %d!!!!", indexTest);
                break;
            }
        }

   }
    return nil;
}



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

    if (indexTest < NUM_OF_PAGES) {
        switch (indexTest) {

            case 0:{
                NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:1] ] );
                indexTest++;
                return [viewControllers objectAtIndex:1];
                break;
            }
            case 1:{
                NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:2] ] );
                indexTest++;
                return [viewControllers objectAtIndex:2];
                break;
            }
            case 2:{
                NSLog(@"No pages AFTER this current page %d", indexTest);
                break;
            }
            default:{
                NSLog(@"PROBLEM  in viewAFTER, indexTest = %d!!!!", indexTest);
                break;
            }
        }

    }
    return nil;

}

最后是页面索引点代码

#pragma mark - dot controller
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    return NUM_OF_PAGES;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return 0;
}

非常感谢任何和所有的帮助,我想我只是在做一些我看不到的愚蠢的事情,因为我已经接近它完全工作了。如果有什么不清楚或我没有提供足够的信息,请告诉我,我会尽我所能回答。

谢谢

4

1 回答 1

5

我认为您的 indexTest 逻辑是错误的,它显示 viewControllers 的索引错误。我不会使用这些方法来跟踪索引,因为即使视图实际上不会更改,也可以调用它们。

相反,您可以像这样替换视图控制器的选择:

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

    int vcIndex = [self.viewControllers indexOfObject:viewController];
    if (vcIndex > 0) {
        return [self.viewControllers objectAtIndex:vcIndex-1];
    }
    return nil;
}

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

    int vcIndex = [self.viewControllers indexOfObject:viewController];
    if (vcIndex < NUM_OF_PAGES-1) {
        return [self.viewControllers objectAtIndex:vcIndex+1];
    }
    return nil;
}

如果您需要跟踪当前索引,请使用提供的委托方法。

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed && finished) {
        UIViewController *currentVC = pageViewController.viewControllers[0];
        indexTest = [self.viewControllers indexOfObject:currentVC];
    }
}

请注意,如果您使用此方法,请不要调用setViewControllers: amimated:YES,因为这将失去正确位置的跟踪。

旁注:我也一直在努力使用 UIPageViewController,发现编写自己的版本更快更容易。我认为它有太多的错误。

于 2013-10-18T15:50:47.270 回答