UIPageViewController不会更改页面

时间:2014-01-30 23:17:01

标签: ios iphone objective-c uipageviewcontroller

我有一个实现UIPageViewControllerDataSource委托的视图控制器,它包含一个UIPageViewController。

我的问题是,经过3个小时的教程和阅读后,我仍然不明白为什么UIPageController通过移动它的内容来对刷卡作出反应,但是如果它没有改变页面的话。滚动就足够了。它始终停留在第一页。

所以这是我的.h文件

#import <UIKit/UIKit.h>
@interface BreathePageViewController : UIViewController <UIPageViewControllerDataSource> 
@property (strong, nonatomic) UIPageViewController *pageController;
@end

这是我的.m文件

#import "BreathePageViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h.h"



@interface BreathePageViewController () {
    NSArray *pageViewControllerScreens;
    FirstViewController *firstViewController;
    SecondViewController *secondViewController;
    int pageIndex;
}

@end

@implementation BreathePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    pageIndex = 0;
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    [self.pageController setViewControllers:@[firstViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self.view addSubview:self.pageController.view];
    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (pageIndex == 0) {
        return nil;
    }
    pageIndex--;
    return [self viewControllerAtIndex:pageIndex];   
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (pageIndex == 2) {
        return nil;
    }
    pageIndex++;        
    return [self viewControllerAtIndex:pageIndex];
}

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

    UIViewController *vc;

    if (pageIndex == 0 ) {
        vc = [[FirstViewController alloc] init];
    }
    else if (pageIndex == 1) {
        vc = [[SecondViewController alloc] init];
    }

    return vc;

}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return 2;
}

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


@end

现在奇怪的是,永远不会调用pageViewController:viewControllerAfterViewController。 有人可以帮我吗? 谢谢

1 个答案:

答案 0 :(得分:6)

经过一夜的睡眠,我将回答我自己的问题。

我不知道为什么(任何评论都赞赏),但问题不是在那个视图控制器上,而是在我创建它的那个。

在父视图控制器中,我正在构建UIPageViewController:

BreathePageViewController  *pageController = [[BreathePageViewController alloc] init];
[pageController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:pageController.view];

现在我尝试将pageViewController的声明作为iVar移动。它有效。 希望能帮助别人