如何正确设置UIPageViewController

时间:2013-11-14 19:56:56

标签: ios objective-c uipageviewcontroller

我在项目中添加了一个UIPageViewController来创建漫游屏幕,但我似乎可以理解如何使用故事板使其工作。

首先,我定义了两种数据源方法:viewControllerBeforeViewControllerviewControllerAfterViewController

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

    NSUInteger index = [(WalktroughScreenPageViewController *)viewController index];

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

    index--;

    return [self viewControllerAtIndex:index];

}

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

    NSUInteger index = [(WalktroughScreenPageViewController *)viewController index];


    index++;

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

    return [self viewControllerAtIndex:index];

}

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

    WalktroughScreenPageViewController *childViewController = [[WalktroughScreenPageViewController alloc] init];
    childViewController.index = index;

    return childViewController;

}

另外,为了显示底部的点,我添加了这两种方法:

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

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

我能理解的是如何在ViewDidLoad中进行设置。我试过类似这样的东西给了我一个EXC_BAD_ACCESS错误:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSource = self;

    WalktroughScreenPageViewController *ic = [self viewControllerAtIndex:0];

    [self setViewControllers:@[ic] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}

有人能指出我正确的方向吗?

0 个答案:

没有答案