切换到UIPageViewControllerTransitionStyleScroll后的空白页

时间:2014-12-29 00:07:11

标签: ios objective-c uipageviewcontroller

我正在尝试将UIPageViewController的过渡方式从UIPageViewControllerTransitionStylePageCurl切换为UIPageViewControllerTransitionStyleScroll

使用UIPageViewControllerTransitionStylePageCurl时,页面会根据需要进行转换。但是,对于UIPageViewControllerTransitionStyleScroll,页面转弯之间会出现空白页面。空白页面显示的模式在构建之间不一致。

知道这里可能会发生什么吗?我很难过。

//AppDelegate.m
PageVC *pageVC = [[PageVC alloc] 
initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal 
options:NULL];

//PageVC.h
@interface PageVC : UIPageViewController <UIPageViewControllerDataSource>

//PageVC.m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.graphVC = [[GraphVC alloc]init];
    self.aboutVC = [[AboutVC alloc] init];
    self.exerciseVC = [[ExerciseVC alloc] init];
    self.viewControllerArray = [[NSArray alloc] initWithObjects:self.exerciseVC, self.graphVC, self.aboutVC, nil];

    self.currentPage = 1;
    if (self.graphVC != nil) {
        self.dataSource = self;

        [self setViewControllers:@[self.graphVC]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO
                      completion:NULL];  
    }}


- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (self.currentPage >= 2) {
        return nil;
    } else {
        self.currentPage += 1;
        return [self.viewControllerArray objectAtIndex:self.currentPage];
    }
}

- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (self.currentPage <= 0) {
        return nil;
    } else {
        self.currentPage -= 1;
        return [self.viewControllerArray objectAtIndex:self.currentPage];
    }
}

1 个答案:

答案 0 :(得分:1)

作为@pbasdf worote,在currentPage / viewControllerAfter中跟踪Before是一个坏主意。使用TransitionStyleScroll时,每页翻页会多次调用这两种方法。结果,它们有时会错误地增加并返回零。 (TransitionStylePageCurl仅在翻页后调用每个方法一次。)

更好的解决方案是检查传递给每个方法的viewController,然后相应地返回:

// PageVC.m

- (void)viewDidLoad {
    [super viewDidLoad];

    // initialize array of ViewControllers
    viewControllerArray = [[NSArray alloc] initWithObjects:exerciseVC, graphVC, aboutVC, nil];
}

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

    NSUInteger index = [viewControllerArray indexOfObject:viewController];
    if (index >= [viewControllerArray count]-1) {
        return nil;
    } else {
        return [viewControllerArray objectAtIndex:index+1];
    }
}

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

    NSUInteger index = [viewControllerArray indexOfObject:viewController];
    if (index <= 0) {
        return nil;
    } else {
        return [viewControllerArray objectAtIndex:index-1];
    }