UIPageViewController消隐页面

时间:2013-10-18 15:09:50

标签: ios xcode nib uipageviewcontroller

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

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

Page1-> Page2-> Page3

然后回到开头:

Page3-> Page2-> Page1。

没问题。问题是如果我滚动,例如从 Page3-> Page2 滚动,然后返回 Page3 ,Page3会在卡入到位时消失。如果我滚动到第四页的位置,那么我得到Page3。这是与UIPageViewController相关的代码,nibs和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;
}

我非常感激任何和所有的帮助,我想我只是做了一些我无法看到的傻事,因为我非常接近它完全正常工作。如果有任何不清楚或我没有提供足够的信息,请告诉我,我会尽我所能回答。

谢谢

1 个答案:

答案 0 :(得分: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,发现编写自己的版本更快更容易。我认为它有太多的错误。