带有ViewControllers的UIPageViewController

时间:2013-07-12 14:29:13

标签: ios viewcontroller

我无法找到带有View Controllers的PageViewController作为页面的工作示例。

我只需要两个水平视图控制器:viewcontroller1和viewcontroller2。

这是我的viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];

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

    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
    controller1 = [storyboard instantiateViewControllerWithIdentifier:@"first"];
    controller2 = [storyboard instantiateViewControllerWithIdentifier:@"second"];

    viewControllers = [NSArray arrayWithObjects:controller1, nil];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    return controller1;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    return controller2;
}

问题是,在滑动第二个视图后,它会消失。 一些帮助理解PageViewController会非常好。

提前致谢

1 个答案:

答案 0 :(得分:5)

更改你这样的委托方法,它应该正常工作:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (self.pageController.viewControllers[0] == self.controller2)
        return self.controller1;
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (self.pageController.viewControllers[0] == self.controller1)
        return self.controller2;
    return nil;
}